Skip to content

Commit

Permalink
helper
Browse files Browse the repository at this point in the history
  • Loading branch information
vvzxy committed Jul 5, 2024
1 parent 0e1604a commit 00e5c23
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 67 deletions.
67 changes: 1 addition & 66 deletions eBPF_Supermarket/CPU_Subsystem/cpu_watcher/cpu_watcher.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

typedef long long unsigned int u64;
typedef unsigned int u32;
#define HASH_SIZE 1024



struct list_head {
Expand Down Expand Up @@ -557,41 +557,7 @@ static int preempt_print(void *ctx, void *data, unsigned long data_sz)
}


typedef struct {
uint64_t ptr;
uint64_t count;
} lock_count_t;

lock_count_t lock_counts[HASH_SIZE];

static uint64_t hash(uint64_t ptr) {
return ptr % HASH_SIZE;
}

static void increment_lock_count(uint64_t ptr) {
uint64_t h = hash(ptr);
while (lock_counts[h].ptr != 0 && lock_counts[h].ptr != ptr) {
h = (h + 1) % HASH_SIZE;
}
if (lock_counts[h].ptr == 0) {
lock_counts[h].ptr = ptr;
lock_counts[h].count = 1;
} else {
lock_counts[h].count++;
}
}

static uint64_t get_lock_count(uint64_t ptr) {
uint64_t h = hash(ptr);
while (lock_counts[h].ptr != 0 && lock_counts[h].ptr != ptr) {
h = (h + 1) % HASH_SIZE;
}
if (lock_counts[h].ptr == 0) {
return 0;
} else {
return lock_counts[h].count;
}
}
//mutrace输出
static int mutrace_print(void *ctx, void *data, unsigned long data_sz) {
const struct mutex_contention_event *e = data;
Expand All @@ -606,38 +572,7 @@ static int mutrace_print(void *ctx, void *data, unsigned long data_sz) {
}


// 定义一个结构来存储已输出的条目
struct output_entry {
int pid;
char comm[16];
long long delay;
};

// 定义一个数组来存储已输出的条目
struct output_entry seen_entries[MAX_ENTRIES];
int seen_count = 0;

// 检查条目是否已存在
bool entry_exists(int pid, const char *comm, long long delay) {
for (int i = 0; i < seen_count; i++) {
if (seen_entries[i].pid == pid &&
strcmp(seen_entries[i].comm, comm) == 0 &&
seen_entries[i].delay == delay) {
return true;
}
}
return false;
}

// 添加条目到已输出的条目列表
void add_entry(int pid, const char *comm, long long delay) {
if (seen_count < MAX_ENTRIES) {
seen_entries[seen_count].pid = pid;
strncpy(seen_entries[seen_count].comm, comm, sizeof(seen_entries[seen_count].comm));
seen_entries[seen_count].delay = delay;
seen_count++;
}
}
static int schedule_print()
{
int err,key = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#define PREEMPT_WACTHER 40
#define SCHEDULE_WACTHER 50
#define MQ_WACTHER 60
#define HASH_SIZE 1024

/*----------------------------------------------*/
/* ewma算法 */
Expand Down Expand Up @@ -64,7 +65,9 @@ bool dynamic_filter(struct ewma_info *ewma_syscall_delay, double dataPoint) {
return 0;
}


/*----------------------------------------------*/
/* bpf file system */
/*----------------------------------------------*/
const char *sar_ctrl_path = "/sys/fs/bpf/cpu_watcher_map/sar_ctrl_map";
const char *cs_ctrl_path = "/sys/fs/bpf/cpu_watcher_map/cs_ctrl_map";
const char *sc_ctrl_path = "/sys/fs/bpf/cpu_watcher_map/sc_ctrl_map";
Expand Down Expand Up @@ -199,4 +202,81 @@ int update_mq_ctrl_map(struct mq_ctrl mq_ctrl){

return 0;
}

/*----------------------------------------------*/
/* mutex_count */
/*----------------------------------------------*/

typedef struct {
uint64_t ptr;
uint64_t count;
} lock_count_t;

lock_count_t lock_counts[HASH_SIZE];

static uint64_t hash(uint64_t ptr) {
return ptr % HASH_SIZE;
}

static void increment_lock_count(uint64_t ptr) {
uint64_t h = hash(ptr);
while (lock_counts[h].ptr != 0 && lock_counts[h].ptr != ptr) {
h = (h + 1) % HASH_SIZE;
}
if (lock_counts[h].ptr == 0) {
lock_counts[h].ptr = ptr;
lock_counts[h].count = 1;
} else {
lock_counts[h].count++;
}
}

static uint64_t get_lock_count(uint64_t ptr) {
uint64_t h = hash(ptr);
while (lock_counts[h].ptr != 0 && lock_counts[h].ptr != ptr) {
h = (h + 1) % HASH_SIZE;
}
if (lock_counts[h].ptr == 0) {
return 0;
} else {
return lock_counts[h].count;
}
}

/*----------------------------------------------*/
/* hash */
/*----------------------------------------------*/


struct output_entry {
int pid;
char comm[16];
long long delay;
};


struct output_entry seen_entries[MAX_ENTRIES];
int seen_count = 0;


bool entry_exists(int pid, const char *comm, long long delay) {
for (int i = 0; i < seen_count; i++) {
if (seen_entries[i].pid == pid &&
strcmp(seen_entries[i].comm, comm) == 0 &&
seen_entries[i].delay == delay) {
return true;
}
}
return false;
}


void add_entry(int pid, const char *comm, long long delay) {
if (seen_count < MAX_ENTRIES) {
seen_entries[seen_count].pid = pid;
strncpy(seen_entries[seen_count].comm, comm, sizeof(seen_entries[seen_count].comm));
seen_entries[seen_count].delay = delay;
seen_count++;
}
}
#endif // CPU_WATCHER_HELPER_H

0 comments on commit 00e5c23

Please sign in to comment.