-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathProcStatus.cc
95 lines (75 loc) · 1.5 KB
/
ProcStatus.cc
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
/*
* SPDX-License-Identifier: GPL-2.0
*
* Copyright (c) 2018 Intel Corporation
*
* Authors: Fengguang Wu <fengguang.wu@intel.com>
*/
#include <linux/limits.h>
#include <string.h>
#include <string>
#include "ProcStatus.h"
void ProcStatus::clear()
{
status_map.clear();
name.clear();
pid = 0;
}
unsigned long ProcStatus::get_number(std::string key) const
{
auto it = status_map.find(key);
if (it != status_map.end())
return it->second;
else
return 0; // kthreadd does not has RssAnon
}
int ProcStatus::load(pid_t n)
{
int rc;
FILE *file;
char filename[PATH_MAX];
pid = n;
snprintf(filename, sizeof(filename), "/proc/%d/status", pid);
file = fopen(filename, "r");
if (!file) {
fprintf(stderr, "open %s failed\n", filename);
return errno;
}
rc = parse_file(file);
fclose(file);
return rc;
}
int ProcStatus::parse_file(FILE *file)
{
int rc;
char line[4096];
while (fgets(line, sizeof(line), file)) {
rc = parse_line(line);
if (rc < 0)
return rc;
}
return 0;
}
int ProcStatus::parse_line(char* line)
{
char* val;
val = strstr(line, ":\t");
if (!val) {
printf("failed to parse status line:\n%s", line);
return -EINVAL;
}
*val++ = '\0';
while (*++val == ' ')
;
if (!strcmp("Name", line)) {
name = val;
name.pop_back(); // remove trailing '\n'
return 0;
}
if (isdigit(val[0])) {
status_map[line] = atoi(val);
// printf("%s = %lu\n", line, status_map[line]);
return 0;
}
return 0;
}