File pacemaker-libcrmcommon-function-get-number-of-CPU-cores.patch of Package pacemaker.14737
commit 89175e75f3b38b10ea163c1a8d621d1296570e7f
Author: Ken Gaillot <kgaillot@redhat.com>
Date: Fri Apr 28 14:30:55 2017 -0500
Feature: libcrmcommon: add function to get number of CPU cores
Compared to the previous implementation in crmd/throttle.c, this
parses /proc/stat, which is smaller than /proc/cpuinfo.
Index: pacemaker/include/crm/common/procfs.h
===================================================================
--- pacemaker.orig/include/crm/common/procfs.h
+++ pacemaker/include/crm/common/procfs.h
@@ -33,5 +33,6 @@
int crm_procfs_process_info(struct dirent *entry, char *name, int *pid);
int crm_procfs_pid_of(const char *name);
+unsigned int crm_procfs_num_cores(void);
#endif /* CRM_COMMON_PROCFS__H */
Index: pacemaker/lib/common/procfs.c
===================================================================
--- pacemaker.orig/lib/common/procfs.c
+++ pacemaker/lib/common/procfs.c
@@ -28,6 +28,7 @@
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
+#include <ctype.h>
/*
* \internal
@@ -140,3 +141,32 @@ crm_procfs_pid_of(const char *name)
closedir(dp);
return pid;
}
+
+/*!
+ * \internal
+ * \brief Calculate number of logical CPU cores from procfs
+ *
+ * \return Number of cores (or 1 if unable to determine)
+ */
+unsigned int
+crm_procfs_num_cores(void)
+{
+ int cores = 0;
+ FILE *stream = NULL;
+
+ /* Parse /proc/stat instead of /proc/cpuinfo because it's smaller */
+ stream = fopen("/proc/stat", "r");
+ if (stream == NULL) {
+ crm_perror(LOG_INFO, "Could not open /proc/stat");
+ } else {
+ char buffer[2048];
+
+ while (fgets(buffer, sizeof(buffer), stream)) {
+ if (!strncmp(buffer, "cpu", 3) && isdigit(buffer[3])) {
+ ++cores;
+ }
+ }
+ fclose(stream);
+ }
+ return cores? cores : 1;
+}