📄 nodelist.c
字号:
/* * Monitor-Server/nodelist.c * * Copyright (C) 2004 Jian Shen <shenjian@net.pku.edu.cn> */#include "nodelist.h"#include "statfile.h"#include <linux/time.h>#include <linux/slab.h>#include <asm/semaphore.h>static struct node_entry* nlist_head = NULL;static struct node_entry* nlist_tail = NULL;struct semaphore nlist_mutex;void nlist_init(){ sema_init(&nlist_mutex, 1);}static void nlist_lock(){ down(&nlist_mutex);}static void nlist_unlock(){ up(&nlist_mutex);}static void add(const char* host_ip, system_load_info* sysload){ struct node_entry* p; struct timeval tm; if(nlist_head == NULL) { nlist_head = (struct node_entry*)kmalloc(sizeof(struct node_entry), GFP_KERNEL); if(nlist_head == NULL) { printk(KERN_ERR "kmalloc failed.\n"); return; } strcpy(nlist_head->host, host_ip); nlist_head->sysload = (system_load_info*)kmalloc(sizeof(system_load_info), GFP_KERNEL); if(nlist_head->sysload == NULL) { kfree(nlist_head); nlist_head = NULL; return; } memcpy(nlist_head->sysload, sysload, sizeof(system_load_info)); do_gettimeofday(&tm); nlist_head->timestamp = tm.tv_sec; nlist_head->prev = NULL; nlist_head->next = NULL; nlist_tail = nlist_head; p = nlist_head; }else { p = (struct node_entry*)kmalloc(sizeof(struct node_entry), GFP_KERNEL); if(p == NULL) { printk(KERN_ERR "kmalloc failed.\n"); return; } strcpy(p->host, host_ip); p->sysload = (system_load_info*)kmalloc(sizeof(system_load_info), GFP_KERNEL); if(p->sysload == NULL) { kfree(p); return; } memcpy(p->sysload, sysload, sizeof(system_load_info)); do_gettimeofday(&tm); p->timestamp = tm.tv_sec; p->prev = nlist_tail; p->next = NULL; nlist_tail->next = p; nlist_tail = p; } /* add a entry in the cluster information directory */ if(create_file(p->host, &(p->dir)) < 0) { printk(KERN_ERR "Create new entry failed.\n");// exit(1); }}static void delete(struct node_entry* p){ if(p == NULL) return; if(p == nlist_head) { nlist_head = p->next; if(p->next == NULL) nlist_tail = NULL; /* only one entry */ else p->next->prev = NULL; }else if(p == nlist_tail) { nlist_tail = p->prev; if(p->prev == NULL) nlist_head = NULL; /* only one entry */ else p->prev->next = NULL; }else { p->prev->next = p->next; p->next->prev = p->prev; } remove_file(p->host, p->dir); kfree(p->sysload); kfree(p);}system_load_info* nlist_get(const char* host_ip){ struct node_entry* p; p = nlist_head; nlist_lock(); while(p != NULL) { if(strcmp(p->host, host_ip) == 0) { nlist_unlock(); return p->sysload; } p = p->next; } nlist_unlock(); return NULL;}/* update the node list */void nlist_update(const char* host_ip, system_load_info* sysload){ struct node_entry* p = nlist_head; struct timeval tm; nlist_lock(); while(p != NULL) { if(strcmp(p->host, host_ip) == 0) { memcpy(p->sysload, sysload, sizeof(system_load_info)); do_gettimeofday(&tm); p->timestamp = tm.tv_sec; /* last modified */// update_content(p->addr, p->sysload); nlist_unlock(); return; } p = p->next; } /* this is a package from a new host */ add(host_ip, sysload); nlist_unlock();}void nlist_check(){ struct node_entry* p; struct timeval tm; p = nlist_head; do_gettimeofday(&tm); nlist_lock(); while(p != NULL) { if(tm.tv_sec - p->timestamp < OBSOLETE_INTERVAL) { p = p->next; }else { if(p->next == NULL) { /* this is the last host */ delete(p); break; } else { p = p->next; delete(p->prev); /* it is obsolete now. */ } } } nlist_unlock();}void nlist_removeall(){ struct node_entry* p; p = nlist_head; nlist_lock(); /* delete all nodes */ while(p != NULL) { if(p->next == NULL) { delete(p); break; } else { p = p->next; delete(p->prev); } } nlist_unlock();}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -