This repository was archived by the owner on Jan 31, 2025. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathProcessServiceUtilsTest.cpp
More file actions
152 lines (128 loc) · 6.32 KB
/
Copy pathProcessServiceUtilsTest.cpp
File metadata and controls
152 lines (128 loc) · 6.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright (c) 2020 The Orbit Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <absl/strings/match.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <unistd.h>
#include <filesystem>
#include <optional>
#include <string>
#include "GrpcProtos/services.pb.h"
#include "OrbitBase/NotFoundOr.h"
#include "OrbitBase/Result.h"
#include "ProcessService/CpuTime.h"
#include "ProcessServiceUtils.h"
#include "Test/Path.h"
#include "TestUtils/TestUtils.h"
namespace orbit_process_service {
using orbit_base::NotFoundOr;
using orbit_grpc_protos::GetDebugInfoFileRequest;
using orbit_test_utils::HasErrorWithMessage;
using orbit_test_utils::HasValue;
TEST(ProcessServiceUtils, GetCumulativeTotalCpuTime) {
// There is not much invariance here which we can test.
// We know the optional should return a value and we know it's positive and
// monotonically increasing.
const auto& total_cpu_time1 = GetCumulativeTotalCpuTime();
ASSERT_TRUE(total_cpu_time1.has_value());
ASSERT_TRUE(total_cpu_time1->jiffies.value > 0ul);
ASSERT_TRUE(total_cpu_time1->cpus > 0ul);
const auto& total_cpu_time2 = GetCumulativeTotalCpuTime();
ASSERT_TRUE(total_cpu_time2.has_value());
ASSERT_TRUE(total_cpu_time2->jiffies.value > 0ul);
ASSERT_TRUE(total_cpu_time2->cpus == total_cpu_time1->cpus);
ASSERT_TRUE(total_cpu_time2->jiffies.value >= total_cpu_time1->jiffies.value);
}
TEST(ProcessServiceUtils, GetCumulativeCpuTimeFromProcess) {
const auto& jiffies1 = GetCumulativeCpuTimeFromProcess(getpid());
ASSERT_TRUE(jiffies1.has_value());
const auto& jiffies2 = GetCumulativeCpuTimeFromProcess(getpid());
ASSERT_TRUE(jiffies2.has_value());
ASSERT_TRUE(jiffies2->value >= jiffies1->value);
const auto& total_cpu_time = GetCumulativeTotalCpuTime();
ASSERT_TRUE(total_cpu_time.has_value());
ASSERT_TRUE(total_cpu_time->jiffies.value > 0ul);
// A single process should never have consumed more CPU cycles than the total CPU time
ASSERT_TRUE(jiffies2->value <= total_cpu_time->jiffies.value);
}
TEST(ProcessServiceUtils, FindSymbolsFilePath) {
const std::filesystem::path test_directory = orbit_test::GetTestdataDir();
{ // elf - same file
const std::filesystem::path module_path = test_directory / "hello_world_elf";
GetDebugInfoFileRequest request;
request.set_module_path(module_path.string());
request.add_additional_search_directories(test_directory);
const ErrorMessageOr<NotFoundOr<std::filesystem::path>> result = FindSymbolsFilePath(request);
ASSERT_THAT(result, HasValue());
ASSERT_FALSE(orbit_base::IsNotFound(result.value()));
const auto result_path = orbit_base::GetFound(result.value());
EXPECT_EQ(result_path, module_path);
}
{ // coff - same file (the coff file actually does not include a build id)
const std::filesystem::path module_path = test_directory / "libtest.dll";
GetDebugInfoFileRequest request;
request.set_module_path(module_path.string());
request.add_additional_search_directories(test_directory);
const ErrorMessageOr<NotFoundOr<std::filesystem::path>> result = FindSymbolsFilePath(request);
ASSERT_THAT(result, HasValue());
ASSERT_FALSE(orbit_base::IsNotFound(result.value()));
const auto result_path = orbit_base::GetFound(result.value());
EXPECT_EQ(result_path, module_path);
}
{ // elf - separate file
const std::filesystem::path module_path = test_directory / "no_symbols_elf";
const std::filesystem::path symbols_path = test_directory / "no_symbols_elf.debug";
GetDebugInfoFileRequest request;
request.set_module_path(module_path.string());
request.add_additional_search_directories(test_directory);
const ErrorMessageOr<NotFoundOr<std::filesystem::path>> result = FindSymbolsFilePath(request);
ASSERT_THAT(result, HasValue());
ASSERT_FALSE(orbit_base::IsNotFound(result.value()));
const auto result_path = orbit_base::GetFound(result.value());
EXPECT_EQ(result_path, symbols_path);
}
{ // coff/pdb - separate file
const std::filesystem::path module_path = test_directory / "dllmain.dll";
const std::filesystem::path symbols_path = test_directory / "dllmain.pdb";
GetDebugInfoFileRequest request;
request.set_module_path(module_path.string());
request.add_additional_search_directories(test_directory);
const ErrorMessageOr<NotFoundOr<std::filesystem::path>> result = FindSymbolsFilePath(request);
ASSERT_THAT(result, HasValue());
ASSERT_FALSE(orbit_base::IsNotFound(result.value()));
const auto result_path = orbit_base::GetFound(result.value());
EXPECT_EQ(result_path, symbols_path);
}
{ // non existing module
const std::filesystem::path module_path = test_directory / "not_existing_file";
GetDebugInfoFileRequest request;
request.set_module_path(module_path.string());
request.add_additional_search_directories(test_directory);
const ErrorMessageOr<NotFoundOr<std::filesystem::path>> result = FindSymbolsFilePath(request);
EXPECT_THAT(result, HasErrorWithMessage("Unable to load object file"));
}
{ // elf - no build id, but does include symbols
const std::filesystem::path module_path = test_directory / "hello_world_elf_no_build_id";
GetDebugInfoFileRequest request;
request.set_module_path(module_path.string());
request.add_additional_search_directories(test_directory);
const ErrorMessageOr<NotFoundOr<std::filesystem::path>> result = FindSymbolsFilePath(request);
ASSERT_THAT(result, HasValue());
ASSERT_FALSE(orbit_base::IsNotFound(result.value()));
const auto result_path = orbit_base::GetFound(result.value());
EXPECT_EQ(result_path, module_path);
}
{ // elf - no build id, no symbols
const std::filesystem::path module_path = test_directory / "no_symbols_no_build_id";
GetDebugInfoFileRequest request;
request.set_module_path(module_path.string());
request.add_additional_search_directories(test_directory);
const ErrorMessageOr<NotFoundOr<std::filesystem::path>> result = FindSymbolsFilePath(request);
ASSERT_THAT(result, HasValue());
ASSERT_TRUE(orbit_base::IsNotFound(result.value()));
EXPECT_TRUE(absl::StrContains(orbit_base::GetNotFoundMessage(result.value()),
"does not contain symbols and does not contain a build id"));
}
}
} // namespace orbit_process_service