Repository navigation
Expand file tree
/
Copy pathProcessInfo.cc
More file actions
240 lines (209 loc) · 4.25 KB
/
Copy pathProcessInfo.cc
File metadata and controls
240 lines (209 loc) · 4.25 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// Copyright 2010, Shuo Chen. All rights reserved.
// http://code.google.com/p/muduo/
//
// Use of this source code is governed by a BSD-style license
// that can be found in the License file.
// Author: Shuo Chen (chenshuo at chenshuo dot com)
//
#include <muduo/base/ProcessInfo.h>
#include <muduo/base/FileUtil.h>
#include <algorithm>
#include <assert.h>
#include <dirent.h>
#include <pwd.h>
#include <stdio.h> // snprintf
#include <stdlib.h>
#include <unistd.h>
#include <sys/resource.h>
#include <sys/times.h>
namespace muduo
{
namespace detail
{
__thread int t_numOpenedFiles = 0;
int fdDirFilter(const struct dirent* d)
{
if (::isdigit(d->d_name[0]))
{
++t_numOpenedFiles;
}
return 0;
}
__thread std::vector<pid_t>* t_pids = NULL;
int taskDirFilter(const struct dirent* d)
{
if (::isdigit(d->d_name[0]))
{
t_pids->push_back(atoi(d->d_name));
}
return 0;
}
int scanDir(const char *dirpath, int (*filter)(const struct dirent *))
{
struct dirent** namelist = NULL;
int result = ::scandir(dirpath, &namelist, filter, alphasort);
assert(namelist == NULL);
return result;
}
Timestamp g_startTime = Timestamp::now();
// assume those won't change during the life time of a process.
int g_clockTicks = static_cast<int>(::sysconf(_SC_CLK_TCK));
int g_pageSize = static_cast<int>(::sysconf(_SC_PAGE_SIZE));
}
}
using namespace muduo;
using namespace muduo::detail;
pid_t ProcessInfo::pid()
{
return ::getpid();
}
string ProcessInfo::pidString()
{
char buf[32];
snprintf(buf, sizeof buf, "%d", pid());
return buf;
}
uid_t ProcessInfo::uid()
{
return ::getuid();
}
string ProcessInfo::username()
{
struct passwd pwd;
struct passwd* result = NULL;
char buf[8192];
const char* name = "unknownuser";
getpwuid_r(uid(), &pwd, buf, sizeof buf, &result);
if (result)
{
name = pwd.pw_name;
}
return name;
}
uid_t ProcessInfo::euid()
{
return ::geteuid();
}
Timestamp ProcessInfo::startTime()
{
return g_startTime;
}
int ProcessInfo::clockTicksPerSecond()
{
return g_clockTicks;
}
int ProcessInfo::pageSize()
{
return g_pageSize;
}
bool ProcessInfo::isDebugBuild()
{
#ifdef NDEBUG
return false;
#else
return true;
#endif
}
string ProcessInfo::hostname()
{
// HOST_NAME_MAX 64
// _POSIX_HOST_NAME_MAX 255
char buf[256];
if (::gethostname(buf, sizeof buf) == 0)
{
buf[sizeof(buf)-1] = '\0';
return buf;
}
else
{
return "unknownhost";
}
}
string ProcessInfo::procname()
{
return procname(procStat()).as_string();
}
StringPiece ProcessInfo::procname(const string& stat)
{
StringPiece name;
size_t lp = stat.find('(');
size_t rp = stat.rfind(')');
if (lp != string::npos && rp != string::npos && lp < rp)
{
name.set(stat.data()+lp+1, static_cast<int>(rp-lp-1));
}
return name;
}
string ProcessInfo::procStatus()
{
string result;
FileUtil::readFile("/proc/self/status", 65536, &result);
return result;
}
string ProcessInfo::procStat()
{
string result;
FileUtil::readFile("/proc/self/stat", 65536, &result);
return result;
}
string ProcessInfo::exePath()
{
string result;
char buf[1024];
ssize_t n = ::readlink("/proc/self/exe", buf, sizeof buf);
if (n > 0)
{
result.assign(buf, n);
}
return result;
}
int ProcessInfo::openedFiles()
{
t_numOpenedFiles = 0;
scanDir("/proc/self/fd", fdDirFilter);
return t_numOpenedFiles;
}
int ProcessInfo::maxOpenFiles()
{
struct rlimit rl;
if (::getrlimit(RLIMIT_NOFILE, &rl))
{
return openedFiles();
}
else
{
return static_cast<int>(rl.rlim_cur);
}
}
ProcessInfo::CpuTime ProcessInfo::cpuTime()
{
ProcessInfo::CpuTime t;
struct tms tms;
if (::times(&tms) >= 0)
{
const double hz = static_cast<double>(clockTicksPerSecond());
t.userSeconds = static_cast<double>(tms.tms_utime) / hz;
t.systemSeconds = static_cast<double>(tms.tms_stime) / hz;
}
return t;
}
int ProcessInfo::numThreads()
{
int result = 0;
string status = procStatus();
size_t pos = status.find("Threads:");
if (pos != string::npos)
{
result = ::atoi(status.c_str() + pos + 8);
}
return result;
}
std::vector<pid_t> ProcessInfo::threads()
{
std::vector<pid_t> result;
t_pids = &result;
scanDir("/proc/self/task", taskDirFilter);
t_pids = NULL;
std::sort(result.begin(), result.end());
return result;
}