src/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
0 / 57
0.0%
Branches:
0 / 156
0.0%
Functions:
0 / 3
0.0%

ps/ps_server.cpp
Line Branch Exec Source
1 #include <csignal>
2 #include <iostream>
3 #include <fstream>
4 #include <string>
5 #include <algorithm>
6 #include <cstdlib>
7
8 #include <gflags/gflags.h>
9
10 #include "base/base.h"
11 #include "base/factory.h"
12 #include "base/init.h"
13 #include "base/json.h"
14 #include "ps/base/base_ps_server.h"
15 #include "src/base/config.h"
16
17 #ifdef ENABLE_PERF_REPORT
18 # include <chrono>
19 # include "base/report/report_client.h"
20 #endif
21
22 #ifdef ENABLE_GPERF_PROFILING
23 # include <gperftools/profiler.h>
24 #endif
25
26 DECLARE_string(config_path);
27 DECLARE_string(brpc_config_path);
28
29 using recstore::BaseParameterServer;
30
31 #ifdef ENABLE_GPERF_PROFILING
32 void StopProfilerAndExit(int signum) {
33 LOG(INFO) << "Caught signal " << signum << ", stopping gperf profiler.";
34 ProfilerStop();
35 std::exit(signum);
36 }
37 #endif
38
39 ✗ static inline std::string ToUpper(std::string s) {
40 ✗ std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c) {
41 ✗ return std::toupper(c);
42 });
43 ✗ return s;
44 }
45
46 ✗ int main(int argc, char** argv) {
47
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ base::Init(&argc, &argv);
48
49 #ifdef ENABLE_GPERF_PROFILING
50 std::signal(SIGINT, StopProfilerAndExit);
51 std::signal(SIGTERM, StopProfilerAndExit);
52
53 struct ProfilerGuard {
54 const char* profile;
55 ProfilerGuard() : profile(std::getenv("CPUPROFILE")) {
56 if (profile) {
57 ProfilerStart(profile);
58 LOG(INFO) << "gperftools CPU profiling started, outputting to "
59 << profile;
60 }
61 }
62 ~ProfilerGuard() {
63 if (profile) {
64 ProfilerStop();
65 LOG(INFO) << "gperftools CPU profiling stopped.";
66 }
67 }
68 } profiler_guard;
69 #endif
70
71
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ std::string cfg_path = FLAGS_config_path;
72 {
73
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ if (!cfg_path.empty()) {
74
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ std::ifstream test(cfg_path);
75
0/4
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ if (!test.good()) {
76 ✗ cfg_path.clear();
77 }
78 ✗ }
79
0/6
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ if (cfg_path.empty() && !FLAGS_brpc_config_path.empty()) {
80
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ std::ifstream test_b(FLAGS_brpc_config_path);
81
0/4
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ if (test_b.good()) {
82
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ cfg_path = FLAGS_brpc_config_path;
83 }
84 ✗ }
85
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ if (cfg_path.empty()) {
86 try {
87
0/4
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ cfg_path = base::ResolveRecStoreConfigPath().string();
88
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ } catch (const std::exception& e) {
89
0/6
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ LOG(ERROR) << e.what();
90 ✗ return 1;
91 ✗ }
92 }
93 }
94
95
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ std::ifstream config_file(cfg_path);
96
0/4
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ if (!config_file.good()) {
97
0/8
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ LOG(ERROR) << "Failed to open config file: " << cfg_path;
98 ✗ return 1;
99 }
100
101 ✗ json config;
102
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ config_file >> config;
103
104
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ std::string ps_type = "GRPC";
105 try {
106
0/12
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ if (config.contains("cache_ps") && config["cache_ps"].contains("ps_type")) {
107
0/6
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ ps_type = config["cache_ps"]["ps_type"].get<std::string>();
108 }
109 ✗ } catch (...) {
110
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ }
111
112 ✗ std::string key;
113
0/4
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ std::string type_upper = ToUpper(ps_type);
114
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ if (type_upper == "GRPC") {
115
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ key = "GRPCParameterServer";
116
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ } else if (type_upper == "BRPC") {
117
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ key = "BRPCParameterServer";
118 } else {
119
0/10
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ LOG(ERROR) << "Unknown ps_type: " << ps_type << ", expected GRPC or BRPC";
120 ✗ return 2;
121 }
122
123
0/14
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ LOG(INFO) << "Using ps_type: " << type_upper << " (key=" << key << ")";
124
0/10
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ LOG(INFO) << "Parameter server config: " << config.dump(2);
125
126 std::unique_ptr<BaseParameterServer> server(
127
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ base::Factory<BaseParameterServer>::NewInstance(key));
128 try {
129 #ifdef ENABLE_PERF_REPORT
130 auto start_time = std::chrono::high_resolution_clock::now();
131 #endif
132
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ server->Init(config);
133 #ifdef ENABLE_PERF_REPORT
134 auto end_time = std::chrono::high_resolution_clock::now();
135 auto duration =
136 std::chrono::duration_cast<std::chrono::microseconds>(
137 end_time - start_time)
138 .count();
139 report(
140 "server_latency", "Init", "latency_us", static_cast<double>(duration));
141 #endif
142
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ server->Run();
143
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ } catch (const std::exception& e) {
144
0/4
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ std::cerr << "FATAL: Uncaught exception in ps_server: " << e.what()
145
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ << std::endl;
146
0/8
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ LOG(FATAL) << "Uncaught exception in ps_server: " << e.what();
147 return 1;
148 ✗ } catch (...) {
149
0/4
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ std::cerr << "FATAL: Unknown exception in ps_server" << std::endl;
150
0/6
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ LOG(FATAL) << "Unknown exception in ps_server";
151 return 1;
152 ✗ }
153
154 ✗ return 0;
155 ✗ }
156