Added Makefile, cleaned up some C code
This commit is contained in:
parent
53f8871381
commit
a5e06d2b93
33
Makefile
Normal file
33
Makefile
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
|
||||||
|
CC = gcc
|
||||||
|
OUT_DIR = ./build
|
||||||
|
TARGET = proxmon
|
||||||
|
SRC_DIRS = ./src
|
||||||
|
|
||||||
|
# Collect all C source files in the current directory
|
||||||
|
SRCS := $(shell find $(SRC_DIRS) -name '*.c')
|
||||||
|
|
||||||
|
# Generate object files in the build directory
|
||||||
|
OBJS := $(patsubst $(SRC_DIRS)/%.c, $(OUT_DIR)/%.o, $(SRCS))
|
||||||
|
|
||||||
|
INC_DIRS := /usr/src/linux-headers-$(uname -r)/include
|
||||||
|
INC_DIRS := $(shell find $(SRC_DIRS) -type d) $(INC_DIRS)
|
||||||
|
INC_FLAGS := $(addprefix -I,$(INC_DIRS))
|
||||||
|
|
||||||
|
CCFLAGS := $(INC_FLAGS)
|
||||||
|
|
||||||
|
$(OUT_DIR):
|
||||||
|
@mkdir -p $@
|
||||||
|
|
||||||
|
$(OUT_DIR)/%.o: $(SRC_DIRS)/%.c | $(OUT_DIR)
|
||||||
|
$(CC) $(CCFLAGS) -c $< -o $@
|
||||||
|
|
||||||
|
$(TARGET): $(OBJS)
|
||||||
|
$(CC) $(CCFLAGS) $(OBJS) -o $@
|
||||||
|
|
||||||
|
.PHONY: all clean
|
||||||
|
all: $(TARGET)
|
||||||
|
|
||||||
|
clean:
|
||||||
|
@rm -rf $(TARGET) $(OUT_DIR)
|
||||||
|
|
||||||
@ -1,33 +0,0 @@
|
|||||||
#include <stdio.h>
|
|
||||||
#include <dirent.h>
|
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
|
||||||
struct dirent *pDirent;
|
|
||||||
DIR *pDir;
|
|
||||||
|
|
||||||
// Ensure correct argument count.
|
|
||||||
|
|
||||||
if (argc != 2) {
|
|
||||||
printf("Usage: testprog <dirname>\n");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ensure we can open directory.
|
|
||||||
|
|
||||||
pDir = opendir(argv[1]);
|
|
||||||
if (pDir == NULL) {
|
|
||||||
printf("Cannot open directory '%s'\n", argv[1]);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Process each entry.
|
|
||||||
|
|
||||||
while ((pDirent = readdir(pDir)) != NULL) {
|
|
||||||
printf("[%s]\n", pDirent->d_name);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Close directory and exit.
|
|
||||||
|
|
||||||
closedir(pDir);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
@ -2,7 +2,7 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main3(int argc, char *argv[]) {
|
||||||
DIR *procdir = opendir("/proc");
|
DIR *procdir = opendir("/proc");
|
||||||
struct dirent *pDirent;
|
struct dirent *pDirent;
|
||||||
|
|
||||||
|
|||||||
13
src/server.c
13
src/server.c
@ -8,6 +8,7 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
|
#include <linux/sched.h>
|
||||||
#include "process.h"
|
#include "process.h"
|
||||||
|
|
||||||
static inline uint64_t fast_str2ull(char** str) {
|
static inline uint64_t fast_str2ull(char** str) {
|
||||||
@ -71,13 +72,14 @@ int parseStatFile(Process *proc, char *filedata) {
|
|||||||
location = strchr(location, ' ') + 1;
|
location = strchr(location, ' ') + 1;
|
||||||
|
|
||||||
// skip [7 - 14)
|
// skip [7 - 14)
|
||||||
skipRange(7, 9);
|
skipRange(7,14);
|
||||||
|
//skipRange(7, 9);
|
||||||
|
|
||||||
// (9) flags - %u
|
// (9) flags - %u
|
||||||
proc->iskernel = fast_str2ull(&location) & PS_KTHREAD;
|
//proc->iskernel = fast_str2ull(&location) & PF_KTHREAD;
|
||||||
location += 1;
|
//location += 1;
|
||||||
|
|
||||||
skipRange(10, 14);
|
//skipRange(10, 14);
|
||||||
|
|
||||||
// (14) utime - %lu
|
// (14) utime - %lu
|
||||||
proc->cpuTime = fast_str2ull(&location);
|
proc->cpuTime = fast_str2ull(&location);
|
||||||
@ -172,6 +174,9 @@ int main(int argc, char *argv[]) {
|
|||||||
cur = head;
|
cur = head;
|
||||||
while (cur != NULL) {
|
while (cur != NULL) {
|
||||||
printf("proc_cpu_time{pid=\"%d\",ppid=\"%d\",label=\"%s\"} %d\n", cur->pid, cur->ppid, cur->label, cur->cpuTime);
|
printf("proc_cpu_time{pid=\"%d\",ppid=\"%d\",label=\"%s\"} %d\n", cur->pid, cur->ppid, cur->label, cur->cpuTime);
|
||||||
|
printf("proc_child_cpu_time{pid=\"%d\",ppid=\"%d\",label=\"%s\"} %d\n", cur->pid, cur->ppid, cur->label, cur->childCpuTime / clocks);
|
||||||
|
printf("proc_num_threads{pid=\"%d\",ppid=\"%d\",label=\"%s\"} %d\n", cur->pid, cur->ppid, cur->label, cur->nThreads);
|
||||||
|
printf("proc_resident_bytes{pid=\"%d\",ppid=\"%d\",label=\"%s\"} %d\n", cur->pid, cur->ppid, cur->label, cur->resident);
|
||||||
cur = cur->next;
|
cur = cur->next;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user