Repository navigation
Expand file tree
/
Copy pathMemoryState.cpp
More file actions
159 lines (129 loc) · 4.24 KB
/
Copy pathMemoryState.cpp
File metadata and controls
159 lines (129 loc) · 4.24 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
153
154
155
156
157
158
159
#include "MemoryState.h"
#include "InferencePipeline.hpp"
#include <stdio.h>
#include <string.h>
#include <fstream>
#include <iostream>
#include <iomanip>
inline int GetCurrentPid() {
return getpid();
}
#define PROCESS_ITEM 14
float getGPUUsage() {
std::ifstream file("/sys/devices/platform/17000000.gpu/load");
if (!file.is_open()) return 0.0f;
std::string line;
std::getline(file, line);
file.close();
try {
return std::stof(line) / 10.0f;
} catch (...) {
return 0.0f;
}
}
int getGPUFrequency() {
std::ifstream file("/sys/devices/platform/17000000.gpu/devfreq/17000000.gpu/cur_freq");
if (!file.is_open()) return 0;
std::string line;
std::getline(file, line);
file.close();
try {
return std::stoi(line) / 1000000;
} catch (...) {
return 0;
}
}
static const char* get_items(const char* buffer, unsigned int item) {
const char* p = buffer;
int len = strlen(buffer);
int count = 0;
for (int i = 0; i < len; i++) {
if (' ' == *p) {
count++;
if (count == item - 1) {
p++;
break;
}
}
p++;
}
return p;
}
static inline unsigned long get_cpu_total_occupy() {
unsigned long user_time, nice_time, system_time, idle_time;
FILE* fd;
char buff[1024] = { 0 };
fd = fopen("/proc/stat", "r");
if (nullptr == fd) return 0;
if (fgets(buff, sizeof(buff), fd)) {
char name[64] = { 0 };
sscanf(buff, "%s %ld %ld %ld %ld", name, &user_time, &nice_time, &system_time, &idle_time);
}
fclose(fd);
return (user_time + nice_time + system_time + idle_time);
}
static inline unsigned long get_cpu_proc_occupy(int pid) {
unsigned int tmp_pid;
unsigned long utime, stime, cutime, cstime;
char file_name[64] = { 0 };
FILE* fd;
char line_buff[1024] = { 0 };
sprintf(file_name, "/proc/%d/stat", pid);
fd = fopen(file_name, "r");
if (nullptr == fd) return 0;
if (fgets(line_buff, sizeof(line_buff), fd)) {
sscanf(line_buff, "%u", &tmp_pid);
const char* q = get_items(line_buff, PROCESS_ITEM);
sscanf(q, "%ld %ld %ld %ld", &utime, &stime, &cutime, &cstime);
}
fclose(fd);
return (utime + stime + cutime + cstime);
}
inline float GetCpuUsageRatio(int pid) {
unsigned long totalcputime1, totalcputime2;
unsigned long procputime1, procputime2;
totalcputime1 = get_cpu_total_occupy();
procputime1 = get_cpu_proc_occupy(pid);
usleep(200000); // 采样间隔 200ms
totalcputime2 = get_cpu_total_occupy();
procputime2 = get_cpu_proc_occupy(pid);
float pcpu = 0.0;
if (0 != (totalcputime2 - totalcputime1)) {
pcpu = (float)(procputime2 - procputime1) / (totalcputime2 - totalcputime1);
}
int cpu_num = get_nprocs();
return pcpu * cpu_num;
}
inline float GetMemoryUsage(int pid) {
char file_name[64] = { 0 };
FILE* fd;
char line_buff[512] = { 0 };
sprintf(file_name, "/proc/%d/status", pid);
fd = fopen(file_name, "r");
if (nullptr == fd) return 0;
char name[64];
int vmrss = 0;
while (fgets(line_buff, sizeof(line_buff), fd)) {
if (strncmp(line_buff, "VmRSS:", 6) == 0) {
sscanf(line_buff, "%s %d", name, &vmrss);
break;
}
}
fclose(fd);
return (float)vmrss / 1024.0;
}
void UseCondition(const InferenceTime& time_data){
// 获取当前进程的 CPU 和 内存 状态
int pid = getpid();
float cpu_usage = GetCpuUsageRatio(pid) * 100.0f;
float mem_usage = GetMemoryUsage(pid);
float gpu_load = getGPUUsage();
int gpu_freq = getGPUFrequency();
std::cout << "------------------------------------------" << std::endl;
std::cout << " CPU Usage : " << std::fixed << std::setprecision(1) << cpu_usage << " %" << std::endl;
std::cout << " MEM Usage : " << mem_usage << " MB" << std::endl;
std::cout << " GPU Load : " << gpu_load << " %" << std::endl;
std::cout << " GPU Freq : " << gpu_freq << " MHz" << std::endl;
std::cout << " [Latency] Total: " << std::fixed << std::setprecision(2) << time_data.total_ms << " ms" << std::endl;
std::cout << "------------------------------------------" << std::endl;
}