Fixed file parsing

This commit is contained in:
dan 2025-01-14 01:43:04 -05:00
parent 0068c483fc
commit 8e99273b42

View File

@ -27,7 +27,7 @@ static inline uint64_t fast_str2ull(char** str) {
return result;
}
static int64_t fast_str2ll(char** str) {
static inline int64_t fast_str2ll(char** str) {
bool neg = false;
if (**str == '-') {
@ -103,22 +103,24 @@ int parseStatFile(Process *proc, char *filedata) {
return 0;
}
inline int findAndParseField(char **filedata, char *fld) {
char *field = strstr(filedata, fld);
while (*(++field) != ':');
while (*(++field) == ' ');
static inline uint64_t findAndParseField(char **filedata, char *fld) {
char *field = strstr(*filedata, fld);
if (!field) return -1;
while (*field < '0' || *field > '9') field++;
*filedata = field;
return fast_str2ull(filedata);
uint64_t val = fast_str2ull(filedata);
return val;
}
int parseStatusFile(Process *proc, char *filedata) {
proc->pid = findAndParseField(&filedata, "Pid");
proc->tgid = findAndParseField(&filedata, "Tgid");
proc->pid = findAndParseField(&filedata, "Pid");
if (proc->pid != proc->tgid) return -1; // ignore child threads, as their stats are the same as the parent
proc->resident = 1024 * findAndParseField(&filedata, "VmRSS");
proc->swap = 1024 * findAndParseField(&filedata, "VmSwap");
return 0;
}
int parseIOFile(Process *proc, char *filedata) {
@ -127,7 +129,7 @@ int parseIOFile(Process *proc, char *filedata) {
return 0;
}
char *readProcesses(char *procdir) {
int readProcesses(char *procdir) {
int len, rc = 1;
struct dirent *pDirent;
DIR *pDir;
@ -136,7 +138,7 @@ char *readProcesses(char *procdir) {
pDir = opendir(procdir);
if (pDir == NULL) {
printf("Cannot open directory '%s'\n", argv[1]);
printf("Cannot open directory '%s'\n", procdir);
return 1;
}
@ -166,7 +168,6 @@ char *readProcesses(char *procdir) {
// truncate 'status' to 'stat'
len = strlen(fname);
fname[len-2] = 0;
file = fopen(fname, "rb");
if (file == NULL) continue;
fread(buffer, 1, 4096, file);
@ -185,6 +186,7 @@ char *readProcesses(char *procdir) {
cur->next->prev = cur;
cur = cur->next;
}
// clean up unused last node
cur = cur->prev;
free(cur->next);
@ -196,8 +198,8 @@ char *readProcesses(char *procdir) {
while (cur != NULL) {
// create process descriptor
sprintf(buffer, "pid=\"%d\",ppid=\"%d\",label=\"%s\",kernel=\"%d\"", cur->pid, cur->ppid, cur->label, cur->iskernel);
printf("proc_cpu_time{%s} %lf\n", buffer, (double) cur->cpuTime / clocks);
printf("proc_child_cpu_time{%s} %lf\n", buffer, (double) cur->childCpuTime / clocks);
printf("proc_cpu_time{%s} %f\n", buffer, (double) cur->cpuTime / clocks);
printf("proc_child_cpu_time{%s} %f\n", buffer, (double) cur->childCpuTime / clocks);
printf("proc_num_threads{%s} %llu\n", buffer, cur->nThreads);
printf("proc_resident_bytes{%s} %llu\n", buffer, cur->resident);
printf("proc_swap_bytes{%s} %llu\n", buffer, cur->swap);
@ -216,6 +218,7 @@ freeChain:
free(prev);
}
closedir(pDir);
free(buffer);
free(fname);
@ -245,8 +248,8 @@ void handle_client(int client_socket) {
int main(int argc, char *argv[]) {
if (argc == 1) {
readProcesses("/proc");
return;
int rc = readProcesses("/proc");
return rc;
}
int server_socket, client_socket;