-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprom_process.c
105 lines (83 loc) · 2.36 KB
/
prom_process.c
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
// Phil Budne
// 2020-04-18
//
// prometheus process vars common on Un*xy systems
// (FreeBSD, macOS, Linux)
#include <sys/time.h> /* gettimeofday */
#include <sys/resource.h> /* getrlimit */
#include <dirent.h> /* opendir... */
#include "prom.h"
#include "common.h"
////////////////
// a gauge in all implementations I've looked at:
PROM_SIMPLE_GAUGE(process_start_time_seconds,
"Start time of the process in seconds since Unix epoch");
// run as a constructor:
static void get_start_time(void) __attribute((constructor));
static void
get_start_time(void) {
// "SIMPLE" variables are integer-only
// so no point in calling gettimeofday()
PROM_SIMPLE_GAUGE_SET(process_start_time_seconds, time(0));
}
////////////////
PROM_GETTER_GAUGE_FN(process_open_fds,
"Number of open file descriptors") {
DIR *d;
static unsigned fds;
DECLARE_LOCK(fds_lock);
static time_t last_fds;
LOCK(fds_lock);
if (STALE(last_fds) &&
(d = opendir("/dev/fd"))) {
struct dirent *dp;
fds = 0;
// readdir_r is deprecated
while ((dp = readdir(d)))
if (dp->d_name[0] != '.')
fds++;
closedir(d);
#ifndef __FreeBSD__ // ?!
fds--; // opendir fd
#endif
}
UNLOCK(fds_lock);
return fds;
}
////////////////
PROM_GETTER_GAUGE_FN(process_max_fds,
"Maximum number of open file descriptors") {
static struct rlimit maxfds;
if (!maxfds.rlim_cur)
getrlimit(RLIMIT_NOFILE, &maxfds);
return maxfds.rlim_cur;
}
////////////////
// Of dubious value : make optional?
// Linux man page says:
// "Since the value is a long, on machines with a 32-bit long either
// this limit is at most 2 GiB, or this resource is unlimited."
PROM_GETTER_GAUGE_FN(process_virtual_memory_max_bytes,
"Maximum amount of virtual memory available in bytes") {
static struct rlimit maxvsz;
if (!maxvsz.rlim_cur)
getrlimit(RLIMIT_AS, &maxvsz);
return maxvsz.rlim_cur;
}
////////////////
#ifdef USE_GETRUSAGE
PROM_GETTER_COUNTER_FN(process_cpu_seconds_total,
"Total user and system CPU time spent in seconds") {
struct rusage ru;
if (getrusage(RUSAGE_SELF, &ru) < 0)
return -1;
return ((ru.ru_utime.tv_sec + ru.ru_stime.tv_sec) +
(ru.ru_utime.tv_usec + ru.ru_stime.tv_usec) / 1000000.0);
}
#endif
////////////////
// called from prom_process_init() to force loading
int
prom_process_common_init(void) {
return 0;
}