Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
B
brpc
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
brpc
Commits
4fc87971
Commit
4fc87971
authored
Apr 12, 2018
by
zhujiashun
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add loadavg, number of opened files, io info support
parent
11bb2868
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
67 additions
and
2 deletions
+67
-2
default_variables.cpp
src/bvar/default_variables.cpp
+67
-2
No files found.
src/bvar/default_variables.cpp
View file @
4fc87971
...
@@ -29,6 +29,10 @@
...
@@ -29,6 +29,10 @@
#include "butil/process_util.h" // ReadCommandLine
#include "butil/process_util.h" // ReadCommandLine
#include <butil/popen.h> // read_command_output
#include <butil/popen.h> // read_command_output
#include "bvar/passive_status.h"
#include "bvar/passive_status.h"
#if defined(OS_MACOSX)
#include <libproc.h>
#include <sys/resource.h>
#endif
namespace
bvar
{
namespace
bvar
{
...
@@ -89,6 +93,7 @@ static bool read_proc_status(ProcStat &stat) {
...
@@ -89,6 +93,7 @@ static bool read_proc_status(ProcStat &stat) {
}
}
return
true
;
return
true
;
#elif defined(OS_MACOSX)
#elif defined(OS_MACOSX)
// TODO(zhujiashun): get remaining state in MacOS.
memset
(
&
stat
,
0
,
sizeof
(
stat
));
memset
(
&
stat
,
0
,
sizeof
(
stat
));
static
pid_t
pid
=
getpid
();
static
pid_t
pid
=
getpid
();
std
::
ostringstream
oss
;
std
::
ostringstream
oss
;
...
@@ -97,7 +102,7 @@ static bool read_proc_status(ProcStat &stat) {
...
@@ -97,7 +102,7 @@ static bool read_proc_status(ProcStat &stat) {
"ps -p %ld -o pid,ppid,pgid,sess"
"ps -p %ld -o pid,ppid,pgid,sess"
",tpgid,flags,pri,nice | tail -n1"
,
(
long
)
pid
);
",tpgid,flags,pri,nice | tail -n1"
,
(
long
)
pid
);
if
(
butil
::
read_command_output
(
oss
,
cmdbuf
)
!=
0
)
{
if
(
butil
::
read_command_output
(
oss
,
cmdbuf
)
!=
0
)
{
LOG
(
ERROR
)
<<
"Fail to read
cmdline
"
;
LOG
(
ERROR
)
<<
"Fail to read
stat
"
;
return
-
1
;
return
-
1
;
}
}
const
std
::
string
&
result
=
oss
.
str
();
const
std
::
string
&
result
=
oss
.
str
();
...
@@ -212,6 +217,7 @@ static bool read_proc_memory(ProcMemory &m) {
...
@@ -212,6 +217,7 @@ static bool read_proc_memory(ProcMemory &m) {
}
}
return
true
;
return
true
;
#elif defined(OS_MACOSX)
#elif defined(OS_MACOSX)
// TODO(zhujiashun): get remaining memory info in MacOS.
memset
(
&
m
,
0
,
sizeof
(
m
));
memset
(
&
m
,
0
,
sizeof
(
m
));
static
pid_t
pid
=
getpid
();
static
pid_t
pid
=
getpid
();
static
int64_t
pagesize
=
getpagesize
();
static
int64_t
pagesize
=
getpagesize
();
...
@@ -219,7 +225,7 @@ static bool read_proc_memory(ProcMemory &m) {
...
@@ -219,7 +225,7 @@ static bool read_proc_memory(ProcMemory &m) {
char
cmdbuf
[
128
];
char
cmdbuf
[
128
];
snprintf
(
cmdbuf
,
sizeof
(
cmdbuf
),
"ps -p %ld -o rss=,vsz="
,
(
long
)
pid
);
snprintf
(
cmdbuf
,
sizeof
(
cmdbuf
),
"ps -p %ld -o rss=,vsz="
,
(
long
)
pid
);
if
(
butil
::
read_command_output
(
oss
,
cmdbuf
)
!=
0
)
{
if
(
butil
::
read_command_output
(
oss
,
cmdbuf
)
!=
0
)
{
LOG
(
ERROR
)
<<
"Fail to read
cmdlin
e"
;
LOG
(
ERROR
)
<<
"Fail to read
memory stat
e"
;
return
-
1
;
return
-
1
;
}
}
const
std
::
string
&
result
=
oss
.
str
();
const
std
::
string
&
result
=
oss
.
str
();
...
@@ -264,6 +270,7 @@ struct LoadAverage {
...
@@ -264,6 +270,7 @@ struct LoadAverage {
};
};
static
bool
read_load_average
(
LoadAverage
&
m
)
{
static
bool
read_load_average
(
LoadAverage
&
m
)
{
#if defined(OS_LINUX)
butil
::
ScopedFILE
fp
(
"/proc/loadavg"
,
"r"
);
butil
::
ScopedFILE
fp
(
"/proc/loadavg"
,
"r"
);
if
(
NULL
==
fp
)
{
if
(
NULL
==
fp
)
{
PLOG_ONCE
(
WARNING
)
<<
"Fail to open /proc/loadavg"
;
PLOG_ONCE
(
WARNING
)
<<
"Fail to open /proc/loadavg"
;
...
@@ -277,6 +284,24 @@ static bool read_load_average(LoadAverage &m) {
...
@@ -277,6 +284,24 @@ static bool read_load_average(LoadAverage &m) {
return
false
;
return
false
;
}
}
return
true
;
return
true
;
#elif defined(OS_MACOSX)
std
::
ostringstream
oss
;
if
(
butil
::
read_command_output
(
oss
,
"sysctl -n vm.loadavg"
)
!=
0
)
{
LOG
(
ERROR
)
<<
"Fail to read loadavg"
;
return
-
1
;
}
const
std
::
string
&
result
=
oss
.
str
();
LOG
(
INFO
)
<<
"result = "
<<
result
;
if
(
sscanf
(
result
.
c_str
(),
"{ %lf %lf %lf }"
,
&
m
.
loadavg_1m
,
&
m
.
loadavg_5m
,
&
m
.
loadavg_15m
)
!=
3
)
{
PLOG
(
WARNING
)
<<
"Fail to sscanf"
;
return
false
;
}
return
true
;
#else
return
false
;
#endif
}
}
class
LoadAverageReader
{
class
LoadAverageReader
{
...
@@ -300,6 +325,7 @@ public:
...
@@ -300,6 +325,7 @@ public:
// ==================================================
// ==================================================
static
int
get_fd_count
(
int
limit
)
{
static
int
get_fd_count
(
int
limit
)
{
#if defined(OS_LINUX)
butil
::
DirReaderPosix
dr
(
"/proc/self/fd"
);
butil
::
DirReaderPosix
dr
(
"/proc/self/fd"
);
int
count
=
0
;
int
count
=
0
;
if
(
!
dr
.
IsValid
())
{
if
(
!
dr
.
IsValid
())
{
...
@@ -310,6 +336,28 @@ static int get_fd_count(int limit) {
...
@@ -310,6 +336,28 @@ static int get_fd_count(int limit) {
// are huge (100k+)
// are huge (100k+)
for
(;
dr
.
Next
()
&&
count
<=
limit
+
3
;
++
count
)
{}
for
(;
dr
.
Next
()
&&
count
<=
limit
+
3
;
++
count
)
{}
return
count
-
3
/* skipped ., .. and the fd in dr*/
;
return
count
-
3
/* skipped ., .. and the fd in dr*/
;
#elif defined(OS_MACOSX)
static
pid_t
pid
=
getpid
();
std
::
ostringstream
oss
;
char
cmdbuf
[
128
];
snprintf
(
cmdbuf
,
sizeof
(
cmdbuf
),
"lsof -p %ld | grep -v
\"
txt
\"
| wc -l"
,
(
long
)
pid
);
if
(
butil
::
read_command_output
(
oss
,
cmdbuf
)
!=
0
)
{
LOG
(
ERROR
)
<<
"Fail to read open files"
;
return
-
1
;
}
const
std
::
string
&
result
=
oss
.
str
();
int
count
=
0
;
if
(
sscanf
(
result
.
c_str
(),
"%d"
,
&
count
)
!=
1
)
{
PLOG
(
WARNING
)
<<
"Fail to sscanf"
;
return
-
1
;
}
// skipped . and first column line
count
=
count
-
2
;
return
std
::
min
(
count
,
limit
);
#else
return
0
;
#endif
}
}
extern
PassiveStatus
<
int
>
g_fd_num
;
extern
PassiveStatus
<
int
>
g_fd_num
;
...
@@ -371,6 +419,7 @@ struct ProcIO {
...
@@ -371,6 +419,7 @@ struct ProcIO {
};
};
static
bool
read_proc_io
(
ProcIO
*
s
)
{
static
bool
read_proc_io
(
ProcIO
*
s
)
{
#if defined(OS_LINUX)
butil
::
ScopedFILE
fp
(
"/proc/self/io"
,
"r"
);
butil
::
ScopedFILE
fp
(
"/proc/self/io"
,
"r"
);
if
(
NULL
==
fp
)
{
if
(
NULL
==
fp
)
{
PLOG_ONCE
(
WARNING
)
<<
"Fail to open /proc/self/io"
;
PLOG_ONCE
(
WARNING
)
<<
"Fail to open /proc/self/io"
;
...
@@ -385,6 +434,22 @@ static bool read_proc_io(ProcIO* s) {
...
@@ -385,6 +434,22 @@ static bool read_proc_io(ProcIO* s) {
return
false
;
return
false
;
}
}
return
true
;
return
true
;
#elif defined(OS_MACOSX)
// TODO(zhujiashun): get rchar, wchar, syscr, syscw, cancelled_write_bytes
// in MacOS.
memset
(
s
,
0
,
sizeof
(
ProcIO
));
static
pid_t
pid
=
getpid
();
rusage_info_current
rusage
;
if
(
proc_pid_rusage
(
pid
,
RUSAGE_INFO_CURRENT
,
(
void
**
)
&
rusage
)
!=
0
)
{
PLOG
(
WARNING
)
<<
"Fail to proc_pid_rusage"
;
return
false
;
}
s
->
read_bytes
=
rusage
.
ri_diskio_bytesread
;
s
->
write_bytes
=
rusage
.
ri_diskio_byteswritten
;
return
true
;
#else
return
false
;
#endif
}
}
class
ProcIOReader
{
class
ProcIOReader
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment