32find_cib_loadfile(
const char *server)
55pcmk__procfs_process_info(
const struct dirent *entry,
char *
name, pid_t *
pid)
60 char procpath[128] = { 0 };
67 local_pid = atoi(entry->d_name);
68 if ((local_pid <= 0) || (strlen(entry->d_name) > 114)) {
72 *
pid = (pid_t) local_pid;
76 strcpy(procpath,
"/proc/");
77 strcat(procpath, entry->d_name);
78 fd = open(procpath, O_RDONLY);
82 if (fstat(fd, &statbuf) < 0) {
89 if (!S_ISDIR(statbuf.st_mode)) {
98 strcat(procpath,
"/status");
99 file = fopen(procpath,
"r");
103 if (fscanf(file,
"Name:\t%15[^\n]",
name) != 1) {
131 struct dirent *entry;
133 char entry_name[64] = { 0 };
135 dp = opendir(
"/proc");
137 crm_notice(
"Can not read /proc directory to track existing components");
141 while ((entry = readdir(dp)) != NULL) {
142 if ((pcmk__procfs_process_info(entry, entry_name, &
pid) ==
pcmk_rc_ok)
172 stream = fopen(
"/proc/stat",
"r");
173 if (stream == NULL) {
174 crm_perror(LOG_INFO,
"Could not open /proc/stat");
178 while (fgets(buffer,
sizeof(buffer), stream)) {
185 return cores? cores : 1;
206 char procfs_exe_path[PATH_MAX];
209 if (snprintf(procfs_exe_path, PATH_MAX,
"/proc/%lld/exe",
210 (
long long)
pid) >= PATH_MAX) {
214 link_rc = readlink(procfs_exe_path,
path, path_size - 1);
217 }
else if (link_rc >= (path_size - 1)) {
221 path[link_rc] =
'\0';
238 static bool have_pids =
false;
239 static bool checked =
false;
269 dir = opendir(
"/proc/self/fd");
286 FILE *procf = fopen(
"/proc/sysrq-trigger",
"a");
289 crm_warn(
"Could not open sysrq-trigger: %s", strerror(errno));
291 fprintf(procf,
"%c\n", t);
344 static char *loadfile = NULL;
345 static time_t last_call = 0;
346 static long ticks_per_s = 0;
347 static unsigned long last_utime, last_stime;
349 char buffer[64*1024];
351 time_t now = time(NULL);
359 if (loadfile == NULL) {
364 loadfile = find_cib_loadfile(server);
365 if (loadfile == NULL) {
366 crm_warn(
"Couldn't find CIB load file");
370 ticks_per_s = sysconf(_SC_CLK_TCK);
374 stream = fopen(loadfile,
"r");
375 if (stream == NULL) {
384 if (fgets(buffer,
sizeof(buffer), stream) != NULL) {
387 int rc = 0,
pid = 0, ppid = 0, pgrp = 0, session = 0, tty_nr = 0, tpgid = 0;
388 unsigned long flags = 0, minflt = 0, cminflt = 0, majflt = 0, cmajflt = 0, utime = 0, stime = 0;
390 rc = sscanf(buffer,
"%d %[^ ] %c %d %d %d %d %d %lu %lu %lu %lu %lu %lu %lu",
391 &
pid, comm, &state, &ppid, &pgrp, &session, &tty_nr, &tpgid,
392 &
flags, &minflt, &cminflt, &majflt, &cmajflt, &utime, &stime);
396 crm_err(
"Only %d of 15 fields found in %s", rc, loadfile);
400 }
else if ((last_call > 0) && (last_call < now) && (last_utime <= utime) &&
401 (last_stime <= stime)) {
402 time_t elapsed = now - last_call;
403 unsigned long delta_utime = utime - last_utime;
404 unsigned long delta_stime = stime - last_stime;
406 *load = delta_utime + delta_stime;
407 *load /= ticks_per_s;
409 crm_debug(
"cib load: %f (%lu ticks in %lds)", *load,
410 delta_utime + delta_stime, (
long) elapsed);
413 crm_debug(
"Init %lu + %lu ticks at %ld (%lu tps)", utime, stime,
414 (
long) now, ticks_per_s);
436 const char *loadfile =
"/proc/loadavg";
442 stream = fopen(loadfile,
"r");
443 if (stream == NULL) {
449 if (fgets(buffer,
sizeof(buffer), stream) != NULL) {
450 char *nl = strstr(buffer,
"\n");
453 *load = strtof(buffer, NULL);
int pcmk__pid_active(pid_t pid, const char *daemon)
#define pcmk__assert_alloc(nmemb, size)
#define crm_info(fmt, args...)
#define crm_warn(fmt, args...)
#define crm_notice(fmt, args...)
#define crm_perror(level, fmt, args...)
Send a system error message to both the log and stderr.
#define crm_debug(fmt, args...)
#define crm_err(fmt, args...)
#define crm_trace(fmt, args...)
bool pcmk__throttle_cib_load(const char *server, float *load)
unsigned int pcmk__procfs_num_cores(void)
bool pcmk__procfs_has_pids(void)
void pcmk__sysrq_trigger(char t)
DIR * pcmk__procfs_fd_dir(void)
bool pcmk__throttle_load_avg(float *load)
pid_t pcmk__procfs_pid_of(const char *name)
int pcmk__procfs_pid2path(pid_t pid, char path[], size_t path_size)
const char * pcmk_rc_str(int rc)
Get a user-friendly description of a return code.
char * crm_strdup_printf(char const *format,...) G_GNUC_PRINTF(1
bool pcmk__starts_with(const char *str, const char *prefix)
Check whether a string starts with a certain sequence.