Skip to content

Commit e4b86ab

Browse files
committed
CFE-30: Add policy variable sys.cpusockets
Ticket: CFE-30 Changelog: Title Signed-off-by: Bastian Triller <bastian.triller@gmail.com>
1 parent 0887b41 commit e4b86ab

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

libenv/sysinfo.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
#include <evalfunction.h>
5555
#include <json-utils.h>
5656
#include <unix.h> /* GetCurrentUserName() */
57+
#include <glob_lib.h>
5758

5859
#ifdef HAVE_ZONE_H
5960
# include <zone.h>
@@ -3059,6 +3060,43 @@ static void GetCPUInfo(EvalContext *ctx)
30593060
count = (int)sysconf(_SC_NPROCESSORS_ONLN);
30603061
#endif
30613062

3063+
#ifdef __linux__
3064+
int package_id = 0, max_package_id = 0;
3065+
char buffer[CF_SMALLBUF] = "";
3066+
StringSet *package_id_files = GlobFileList("/sys/devices/system/cpu/cpu*/topology/physical_package_id");
3067+
StringSetIterator it = StringSetIteratorInit(package_id_files);
3068+
const char *file = NULL;
3069+
3070+
while ((file = StringSetIteratorNext(&it)))
3071+
{
3072+
int f = open(file, O_RDONLY);
3073+
if (f == -1)
3074+
continue;
3075+
ssize_t n_read = FullRead(f, buffer, sizeof(buffer));
3076+
if (n_read < 1)
3077+
continue;
3078+
if (sscanf(buffer, "%d", &package_id) == 1)
3079+
{
3080+
if (package_id > max_package_id)
3081+
max_package_id = package_id;
3082+
}
3083+
}
3084+
max_package_id++;
3085+
StringSetDestroy(package_id_files);
3086+
3087+
if (max_package_id == 1)
3088+
{
3089+
snprintf(buffer, CF_SMALLBUF, "%d_cpusocket", max_package_id);
3090+
}
3091+
else
3092+
{
3093+
snprintf(buffer, CF_SMALLBUF, "%d_cpusockets", max_package_id);
3094+
}
3095+
EvalContextClassPutHard(ctx, buffer, "source=agent,derived-from=sys.cpusockets");
3096+
snprintf(buffer, CF_SMALLBUF, "%d", max_package_id);
3097+
EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_SYS, "cpusockets", buffer, CF_DATA_TYPE_STRING, "inventory,source=agent,attribute_name=CPU sockets");
3098+
#endif
3099+
30623100
#if defined(HAVE_SYS_SYSCTL_H) && defined(HW_NCPU)
30633101
// BSD-derived platforms
30643102
int mib[2] = { CTL_HW, HW_NCPU };

0 commit comments

Comments
 (0)