modify module to tree

main
hwwang 6 months ago
parent 63f689cba7
commit 75bf451df0

@ -94,7 +94,8 @@
"stdbool.h": "c", "stdbool.h": "c",
"compare": "c", "compare": "c",
"cstdint": "c", "cstdint": "c",
"format": "c" "format": "c",
"jsmn.h": "c"
}, },
"files.exclude": { "files.exclude": {
"**/.git": true, "**/.git": true,

@ -114,6 +114,12 @@
if (__sync_bool_compare_and_swap(&deque->top, ct, ct + 1) == false) return -EAGAIN; \ if (__sync_bool_compare_and_swap(&deque->top, ct, ct + 1) == false) return -EAGAIN; \
\ \
return 0; \ return 0; \
} \
static inline bool deque_is_empty_##name(struct deque_##name *q) \
{ \
long ct = q->top; \
long cb = q->bottom; \
return ct >= cb; \
} }
#endif /* DEQUE_H */ #endif /* DEQUE_H */

@ -1,4 +1,4 @@
#pragma once #pragma 78once
#include <string.h> #include <string.h>
#include <sys/socket.h> #include <sys/socket.h>
@ -20,6 +20,7 @@
#define MODULE_DEFAULT_REQUEST_RESPONSE_SIZE (PAGE_SIZE) #define MODULE_DEFAULT_REQUEST_RESPONSE_SIZE (PAGE_SIZE)
#define MODULE_MAX_ARGUMENT_COUNT 16 #define MODULE_MAX_ARGUMENT_COUNT 16
#define MODULE_MAX_ARGUMENT_SIZE 64 #define MODULE_MAX_ARGUMENT_SIZE 64
#define MODULE_MAX_NAME_LENGTH 32 #define MODULE_MAX_NAME_LENGTH 32
@ -84,11 +85,8 @@ struct module {
/* the left and right children module in the tree */ /* the left and right children module in the tree */
struct module *left_module; struct module *left_module;
struct module *right_module; struct module *right_module;
/* the parent moudle info*/ /* parent moudle or not?*/
struct module *parent_module; bool *is_parent;
{
/* data */
};
}; };

@ -225,7 +225,8 @@ sandbox_mem_print_perf(struct sandbox *sandbox)
uint64_t blocked_us = sandbox->blocked_duration / runtime_processor_speed_MHz; uint64_t blocked_us = sandbox->blocked_duration / runtime_processor_speed_MHz;
uint64_t returned_us = sandbox->returned_duration / runtime_processor_speed_MHz; uint64_t returned_us = sandbox->returned_duration / runtime_processor_speed_MHz;
if (sandbox->module->next_module == NULL) { //if (sandbox->module->next_module == NULL) {
if(sandbox->module->is_parent) {
uint64_t total_time = (sandbox->completion_timestamp - sandbox->request_arrival_timestamp) / runtime_processor_speed_MHz; uint64_t total_time = (sandbox->completion_timestamp - sandbox->request_arrival_timestamp) / runtime_processor_speed_MHz;
bool miss_deadline = sandbox->completion_timestamp > sandbox->absolute_deadline ? true : false; bool miss_deadline = sandbox->completion_timestamp > sandbox->absolute_deadline ? true : false;
uint64_t delayed_us = (sandbox->completion_timestamp - sandbox->absolute_deadline) uint64_t delayed_us = (sandbox->completion_timestamp - sandbox->absolute_deadline)

@ -352,7 +352,7 @@ main(int argc, char **argv)
memset(runtime_worker_threads, 0, sizeof(pthread_t) * RUNTIME_WORKER_THREAD_CORE_COUNT); memset(runtime_worker_threads, 0, sizeof(pthread_t) * RUNTIME_WORKER_THREAD_CORE_COUNT);
//runtime_processor_speed_MHz = runtime_get_processor_speed_MHz(); //runtime_processor_speed_MHz = runtime_get_processor_speed_MHz();
runtime_processor_speed_MHz = 2600; //this is the default value runtime_processor_speed_MHz = 2400; //this is the default value
char *cpu_speed_MHz_raw = getenv("SLEDGE_CPU_SPEED"); char *cpu_speed_MHz_raw = getenv("SLEDGE_CPU_SPEED");
if (cpu_speed_MHz_raw != NULL) { if (cpu_speed_MHz_raw != NULL) {
long cpu_speed_MHz = atoi(cpu_speed_MHz_raw); long cpu_speed_MHz = atoi(cpu_speed_MHz_raw);

@ -19,7 +19,8 @@
const int JSON_MAX_ELEMENT_COUNT = 16; const int JSON_MAX_ELEMENT_COUNT = 16;
const int JSON_MAX_ELEMENT_SIZE = 1024; const int JSON_MAX_ELEMENT_SIZE = 1024;
DEQUE_PROTOTYPE(Treequeue,struct module *)
static struct deque_Treequeue *tree_queue;
/************************* /*************************
* Private Static Inline * * Private Static Inline *
************************/ ************************/
@ -374,7 +375,26 @@ module_new_from_json(char *file_name)
int module_count = 0; int module_count = 0;
char *request_headers = NULL; char *request_headers = NULL;
char *reponse_headers = NULL; char *reponse_headers = NULL;
struct module *tail_module = NULL; /* struct module *tail_module = NULL;
deque_init_Treequeue(tree_queue, total_tokens);
assert(tokens[0].type == JSMN_OBJECT);
while (deque_is_empty_Treequeue(tree_queue))
{
Node *current = dequeue(&queue);
if (is_parent) {
current->left = getNextNode();
enqueue(&queue, current->left);
}
if (is_parent) {
current->right = getNextNode();
enqueue(&queue, current->right);
}
}
}*/
struct module *nodes[total_tokens];
for (int i = 0; i < total_tokens; i++) { for (int i = 0; i < total_tokens; i++) {
assert(tokens[i].type == JSMN_OBJECT); assert(tokens[i].type == JSMN_OBJECT);
@ -454,7 +474,18 @@ module_new_from_json(char *file_name)
} else { } else {
panic("Expected active key to be a JSON boolean, was %s\n", val); panic("Expected active key to be a JSON boolean, was %s\n", val);
} }
} else if (strcmp(key, "tail-module") == 0) { }else if(strcmp(key,"is_parent") == 0)
{
assert(tokens[i + j + 1].type == JSMN_PRIMITIVE);
if (val[0] == 't') {
is_tail_module = true;
} else if (val[0] == 'f') {
is_tail_module = false;
} else {
panic("Expected is-parent key to be a JSON boolean, was %s\n", val);
}
}
/*else if (strcmp(key, "tail-module") == 0) {
assert(tokens[i + j + 1].type == JSMN_PRIMITIVE); assert(tokens[i + j + 1].type == JSMN_PRIMITIVE);
if (val[0] == 't') { if (val[0] == 't') {
is_tail_module = true; is_tail_module = true;
@ -463,7 +494,7 @@ module_new_from_json(char *file_name)
} else { } else {
panic("Expected tail_module key to be a JSON boolean, was %s\n", val); panic("Expected tail_module key to be a JSON boolean, was %s\n", val);
} }
} else if (strcmp(key, "relative-deadline-us") == 0) { }*/else if (strcmp(key, "relative-deadline-us") == 0) {
int64_t buffer = strtoll(val, NULL, 10); int64_t buffer = strtoll(val, NULL, 10);
if (buffer < 0 || buffer > (int64_t)RUNTIME_RELATIVE_DEADLINE_US_MAX) if (buffer < 0 || buffer > (int64_t)RUNTIME_RELATIVE_DEADLINE_US_MAX)
panic("Relative-deadline-us must be between 0 and %ld, was %ld\n", panic("Relative-deadline-us must be between 0 and %ld, was %ld\n",
@ -568,15 +599,18 @@ module_new_from_json(char *file_name)
if (module == NULL) goto module_new_err; if (module == NULL) goto module_new_err;
assert(module); assert(module);
if(is_tail_module) module->is_parent == true;
nodes[i] = malloc(sizeof(module));
nodes[i] = module;
if (tail_module != NULL) { tail_module->next_module = module; } // if (tail_module != NULL) { tail_module->next_module = module; }
tail_module = module; // tail_module = module;
tail_module->next_module = NULL; // tail_module->next_module = NULL;
/* if this is the tail module, reset tail_module to NULL to build another new chain */ // /* if this is the tail module, reset tail_module to NULL to build another new chain */
if (is_tail_module) { // if (is_tail_module) {
tail_module = NULL; // tail_module = NULL;
} // }
module_set_http_info(module, request_count, request_headers, request_content_type, module_set_http_info(module, request_count, request_headers, request_content_type,
response_count, reponse_headers, response_content_type); response_count, reponse_headers, response_content_type);
@ -586,7 +620,12 @@ module_new_from_json(char *file_name)
free(request_headers); free(request_headers);
free(reponse_headers); free(reponse_headers);
} }
for (int i = 0; i < total_tokens; i++) {
int left_index = 2 * i + 1;
int right_index = 2 * i + 2;
if (left_index < total_tokens) nodes[i]->left_module = nodes[left_index];
if (right_index < total_tokens) nodes[i]->right_module = nodes[right_index];
}
if (module_count == 0) panic("%s contained no active modules\n", file_name); if (module_count == 0) panic("%s contained no active modules\n", file_name);
#ifdef LOG_MODULE_LOADING #ifdef LOG_MODULE_LOADING
debuglog("Loaded %d module%s!\n", module_count, module_count > 1 ? "s" : ""); debuglog("Loaded %d module%s!\n", module_count, module_count > 1 ? "s" : "");

@ -1,50 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
typedef struct TreeNode {
void (*func)(struct TreeNode *); // 节点的函数
struct TreeNode *left; // 左子节点
struct TreeNode *right; // 右子节点
} TreeNode;
void nodeFunction(TreeNode *node) {
printf("Node function executed: %p\n", (void *)node);
}
void leafFunction(TreeNode *node) {
printf("Leaf function executed: %p\n", (void *)node);
}
TreeNode *createBinaryTree(int depth) {
if (depth == 0) return NULL;
TreeNode *node = (TreeNode *)malloc(sizeof(TreeNode));
node->func = (depth > 1) ? nodeFunction : leafFunction; // 叶节点使用 leafFunction
node->left = createBinaryTree(depth - 1);
node->right = createBinaryTree(depth - 1);
return node;
}
void executeTree(TreeNode *node) {
if (node == NULL) return;
// 首先递归执行左右子节点
executeTree(node->left);
executeTree(node->right);
// 然后执行当前节点的函数
node->func(node);
}
void freeTree(TreeNode *node) {
if (node == NULL) return;
freeTree(node->left);
freeTree(node->right);
free(node);
}
int main() {
// 创建一个深度为 3 的二叉树
TreeNode *root = createBinaryTree(3);
// 执行二叉树
executeTree(root);
// 释放二叉树占用的内存
freeTree(root);
return 0;
}
Loading…
Cancel
Save