Testing calloc performance

This commit is contained in:
Dan Snyder 2025-01-14 14:09:26 -05:00
parent b7e1868f24
commit 01d4d93876
3 changed files with 15 additions and 22 deletions

View File

@ -1,19 +1,20 @@
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include "lxcstat.h" #include "lxcstat.h"
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include "parsing.h" #include "parsing.h"
int parseLxcConfigFile(lxcinfo *lxc, char *filedata) { int parseLxcConfigFile(lxcinfo *lxc, char *filedata) {
lxc->cpucount = 0;
lxc->memlimit = 0;
char *temp = filedata; char *temp = filedata;
lxc->cpucount = findAndParseField(&temp, "cores"); lxc->cpucount = findAndParseField(&temp, "cores");
temp = filedata; temp = filedata;
lxc->memlimit = findAndParseField(&temp, "memory") * 1024 * 1024; lxc->memlimit = findAndParseField(&temp, "memory") * 1024 * 1024;
temp = filedata;
lxc->swaplimit = findAndParseField(&temp, "swap") * 1024 * 1024;
char *ptr = strstr(filedata, "hostname:"); char *ptr = strstr(filedata, "hostname:");
ptr += strlen("hostname:"); ptr += strlen("hostname:");
@ -48,7 +49,7 @@ lxcinfo *getLXCInfo(lxcinfo **lxcs, int lxcid) {
lxc = lxc->next; lxc = lxc->next;
} }
lxc = malloc(sizeof(lxcinfo)); lxc = calloc(1, sizeof(lxcinfo));
lxc->lxcid = lxcid; lxc->lxcid = lxcid;
if (readLxcConfig(lxc, lxcid)) { if (readLxcConfig(lxc, lxcid)) {

View File

@ -8,6 +8,7 @@ typedef struct _lxc_info {
int lxcid; int lxcid;
int cpucount; int cpucount;
int memlimit; int memlimit;
int swaplimit;
char hostname[128]; char hostname[128];
} lxcinfo; } lxcinfo;

View File

@ -61,16 +61,8 @@ int parseStatFile(Process *proc, char *filedata) {
proc->cpuTime += fast_str2ull(&location); proc->cpuTime += fast_str2ull(&location);
location += 1; location += 1;
// (16) cutime - %lu // skip 16-19
proc->childCpuTime = fast_str2ull(&location); skipRange(16, 20);
location += 1;
// (17) cstime - %lu
proc->childCpuTime += fast_str2ull(&location);
location += 1;
// skip 18-19
skipRange(18, 20);
// (20) num_threads - %ld // (20) num_threads - %ld
proc->nThreads = fast_str2ull(&location); proc->nThreads = fast_str2ull(&location);
@ -118,9 +110,7 @@ char *readProcesses(char *procdir) {
char first; char first;
FILE *file; FILE *file;
Process *cur = malloc(sizeof(Process)); Process *cur = calloc(1, sizeof(Process));
cur->prev = NULL;
cur->next = NULL;
Process *head = cur; Process *head = cur;
lxcinfo *lxcs = NULL; lxcinfo *lxcs = NULL;
@ -172,7 +162,7 @@ char *readProcesses(char *procdir) {
} }
nextProcess: nextProcess:
cur->next = malloc(sizeof(Process)); cur->next = calloc(1, sizeof(Process));
cur->next->prev = cur; cur->next->prev = cur;
cur = cur->next; cur = cur->next;
} }
@ -211,6 +201,7 @@ char *readProcesses(char *procdir) {
sprintf(buffer, "lxc=\"%d\",lxcname=\"%s\"", lxc->lxcid, lxc->hostname); sprintf(buffer, "lxc=\"%d\",lxcname=\"%s\"", lxc->lxcid, lxc->hostname);
ptr += sprintf(ptr, "lxc_cpu_core_count{%s} %u\n", buffer, lxc->cpucount); ptr += sprintf(ptr, "lxc_cpu_core_count{%s} %u\n", buffer, lxc->cpucount);
ptr += sprintf(ptr, "lxc_memory_limit_bytes{%s} %llu\n", buffer, lxc->memlimit); ptr += sprintf(ptr, "lxc_memory_limit_bytes{%s} %llu\n", buffer, lxc->memlimit);
ptr += sprintf(ptr, "lxc_swap_limit_bytes{%s} %llu\n", buffer, lxc->swaplimit);
// free and proceed // free and proceed
lxcinfo *prev = lxc; lxcinfo *prev = lxc;
lxc = lxc->next; lxc = lxc->next;