Compare commits
8 Commits
Author | SHA1 | Date |
---|---|---|
hwwang | ef88c3f960 | 5 months ago |
hwwang | 5161282992 | 5 months ago |
hwwang | f766d17fb0 | 5 months ago |
hwwang | dcfab6f1c0 | 6 months ago |
hwwang | cc7b810fd0 | 6 months ago |
hwwang | 793c12c9b9 | 6 months ago |
hwwang | 2fd2b99319 | 6 months ago |
hwwang | bc6a861e77 | 6 months ago |
@ -1,52 +1,85 @@
|
||||
{
|
||||
// Use IntelliSense to learn about possible attributes.
|
||||
// Hover to view descriptions of existing attributes.
|
||||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"name": "Hyde",
|
||||
"type": "cppdbg",
|
||||
"request": "launch",
|
||||
"program": "${workspaceFolder}/runtime/bin/sledgert",
|
||||
"args": [
|
||||
"${workspaceFolder}/runtime/experiments/applications/ocr/hyde/spec.json"
|
||||
],
|
||||
"stopAtEntry": false,
|
||||
"cwd": "${workspaceFolder}",
|
||||
"environment": [],
|
||||
"externalConsole": false,
|
||||
"MIMode": "gdb",
|
||||
"envFile": "${workspaceFolder}/.env",
|
||||
"setupCommands": [
|
||||
{
|
||||
"description": "Enable pretty-printing for gdb",
|
||||
"text": "-enable-pretty-printing",
|
||||
"ignoreFailures": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "Preemption",
|
||||
"type": "cppdbg",
|
||||
"request": "launch",
|
||||
"program": "${workspaceFolder}/runtime/bin/sledgert",
|
||||
"args": [
|
||||
"${workspaceFolder}/runtime/experiments/preemption/spec.json"
|
||||
],
|
||||
"stopAtEntry": false,
|
||||
"cwd": "${workspaceFolder}",
|
||||
"environment": [],
|
||||
"externalConsole": false,
|
||||
"MIMode": "gdb",
|
||||
"envFile": "${workspaceFolder}/.env",
|
||||
"setupCommands": [
|
||||
{
|
||||
"description": "Enable pretty-printing for gdb",
|
||||
"text": "-enable-pretty-printing",
|
||||
"ignoreFailures": true
|
||||
}
|
||||
]
|
||||
}
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"name": "Hyde",
|
||||
"type": "cppdbg",
|
||||
"request": "launch",
|
||||
"program": "${workspaceFolder}/runtime/bin/sledgert",
|
||||
"args": [
|
||||
"${workspaceFolder}/runtime/experiments/applications/ocr/hyde/spec.json"
|
||||
],
|
||||
"stopAtEntry": false,
|
||||
"cwd": "${workspaceFolder}",
|
||||
"environment": [
|
||||
{
|
||||
"name": "SLEDGE_SANDBOX_PERF_LOG",
|
||||
"value": "${workspaceFolder}/debug.log"
|
||||
}
|
||||
],
|
||||
"externalConsole": false,
|
||||
"MIMode": "gdb",
|
||||
"envFile": "${workspaceFolder}/.env",
|
||||
"sourceFileMap": {
|
||||
"/sledge/runtime": "${workspaceFolder}/runtime"
|
||||
},
|
||||
"setupCommands": [
|
||||
{
|
||||
"description": "Enable pretty-printing for gdb",
|
||||
"text": "-enable-pretty-printing",
|
||||
"ignoreFailures": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "Preemption",
|
||||
"type": "cppdbg",
|
||||
"request": "launch",
|
||||
"program": "${workspaceFolder}/runtime/bin/sledgert",
|
||||
"args": [
|
||||
"${workspaceFolder}/runtime/tests/test_multiple_image_processing4.json"
|
||||
],
|
||||
"stopAtEntry": false,
|
||||
"cwd": "${workspaceFolder}",
|
||||
"environment": [],
|
||||
"externalConsole": false,
|
||||
"MIMode": "gdb",
|
||||
"sourceFileMap": {
|
||||
"/sledge/runtime": "${workspaceFolder}/runtime"
|
||||
},
|
||||
"envFile": "${workspaceFolder}/.env",
|
||||
"setupCommands": [
|
||||
{
|
||||
"description": "Enable pretty-printing for gdb",
|
||||
"text": "-enable-pretty-printing",
|
||||
"ignoreFailures": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "tree",
|
||||
"type": "cppdbg",
|
||||
"request": "launch",
|
||||
"program": "${workspaceFolder}/runtime/bin/sledgert",
|
||||
"args": [
|
||||
"${workspaceFolder}/runtime/tests/tree.json"
|
||||
],
|
||||
"stopAtEntry": false,
|
||||
"cwd": "${workspaceFolder}",
|
||||
"environment": [],
|
||||
"externalConsole": false,
|
||||
"MIMode": "gdb",
|
||||
"sourceFileMap": {
|
||||
"/sledge/runtime": "${workspaceFolder}/runtime"
|
||||
},
|
||||
"envFile": "${workspaceFolder}/.env",
|
||||
"setupCommands": [
|
||||
{
|
||||
"description": "Enable pretty-printing for gdb",
|
||||
"text": "-enable-pretty-printing",
|
||||
"ignoreFailures": true
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,88 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define TABLE_SIZE 100
|
||||
|
||||
typedef struct Node {
|
||||
int key;
|
||||
char value[256];
|
||||
struct Node* next;
|
||||
} Node;
|
||||
|
||||
typedef struct Edge {
|
||||
Node* from;
|
||||
Node* to;
|
||||
double cost;
|
||||
struct Edge* next;
|
||||
} Edge;
|
||||
|
||||
typedef struct Graph {
|
||||
Node* nodes[TABLE_SIZE];
|
||||
Edge* edges[TABLE_SIZE];
|
||||
} Graph;
|
||||
|
||||
unsigned int hash(int key) {
|
||||
return key % TABLE_SIZE;
|
||||
}
|
||||
|
||||
void insertNode(Graph* g, int key, const char* value) {
|
||||
unsigned int index = hash(key);
|
||||
Node* new_node = (Node*) malloc(sizeof(Node));
|
||||
if (new_node) {
|
||||
new_node->key = key;
|
||||
strcpy(new_node->value, value);
|
||||
new_node->next = g->nodes[index];
|
||||
g->nodes[index] = new_node;
|
||||
}
|
||||
}
|
||||
|
||||
Node* findNode(Graph* g, int key) {
|
||||
unsigned int index = hash(key);
|
||||
Node* node = g->nodes[index];
|
||||
while (node) {
|
||||
if (node->key == key)
|
||||
return node;
|
||||
node = node->next;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void insertEdge(Graph* g, int fromKey, int toKey, double cost) {
|
||||
Node* fromNode = findNode(g, fromKey);
|
||||
Node* toNode = findNode(g, toKey);
|
||||
if (fromNode && toNode) {
|
||||
unsigned int index = hash(fromKey);
|
||||
Edge* new_edge = (Edge*) malloc(sizeof(Edge));
|
||||
if (new_edge) {
|
||||
new_edge->from = fromNode;
|
||||
new_edge->to = toNode;
|
||||
new_edge->cost = cost;
|
||||
new_edge->next = g->edges[index];
|
||||
g->edges[index] = new_edge;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void initGraph(Graph* g) {
|
||||
for (int i = 0; i < TABLE_SIZE; i++) {
|
||||
g->nodes[i] = NULL;
|
||||
g->edges[i] = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
Graph g;
|
||||
initGraph(&g);
|
||||
|
||||
insertNode(&g, 1, "Node 1");
|
||||
insertNode(&g, 2, "Node 2");
|
||||
insertEdge(&g, 1, 2, 0.5);
|
||||
|
||||
Node* n = findNode(&g, 1);
|
||||
if (n) {
|
||||
printf("Found node: %s\n", n->value);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,53 @@
|
||||
import unittest,importlib,BeautifulReport
|
||||
import cffi
|
||||
|
||||
def load():
|
||||
with open("/home/hai/sledge-serverless-framework/runtime/include/hash.h", "r") as f:
|
||||
inc = ""
|
||||
for line in f:
|
||||
if not line.strip().startswith('#'):
|
||||
inc += line
|
||||
src = open("/home/hai/sledge-serverless-framework/runtime/Utest_py/hash.c").read()
|
||||
|
||||
builder = cffi.FFI()
|
||||
|
||||
builder.cdef(inc)
|
||||
builder.set_source("hashlib",src)
|
||||
builder.compile()
|
||||
|
||||
md = importlib.import_module("hashlib")
|
||||
|
||||
return md.lib
|
||||
|
||||
md = load()
|
||||
class HashTableTestCase(unittest.TestCase):
|
||||
|
||||
def test_case1(self):
|
||||
'''
|
||||
测试添加和查找功能
|
||||
'''
|
||||
table = md.create_table()
|
||||
md.add_item(table, b"key1", "Hello World")
|
||||
value = md.find_value(table, b"key1")
|
||||
self.assertEqual(value, "Hello World")
|
||||
print('Value for "key1":', value)
|
||||
|
||||
def test_case2(self):
|
||||
'''
|
||||
测试查找不存在的键
|
||||
'''
|
||||
table = md.create_table()
|
||||
value = md.find_value(table, b"nonexistent")
|
||||
self.assertIsNone(value)
|
||||
print('Value for "nonexistent":', value)
|
||||
|
||||
def test_case3(self):
|
||||
# 确保每个测试后表被释放
|
||||
md.free_table(self.table)
|
||||
|
||||
sut = unittest.TestSuite()
|
||||
sut.addTest(unittest.makeSuite(HashTableTestCase))
|
||||
run = BeautifulReport.BeautifulReport(sut)
|
||||
|
||||
run.report(filename="test.html",description="单元测试")
|
||||
|
Binary file not shown.
@ -0,0 +1,49 @@
|
||||
// hash_test.c
|
||||
#include "../include/hash.h"
|
||||
#include <stdio.h>
|
||||
typedef struct test
|
||||
{
|
||||
char *name;
|
||||
}test;
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
test *test1 = malloc(sizeof(test));
|
||||
test *test2 = malloc(sizeof(test));
|
||||
test1->name = "test1";
|
||||
test2->name = "test2";
|
||||
HashTable *myTable = create_table();
|
||||
add_item(myTable, "key1", test1);
|
||||
add_item(myTable, "key2", test2);
|
||||
char *input = argv[1];
|
||||
test *value = find_value(myTable, input);
|
||||
if (value) {
|
||||
printf("Test Passed: %s\n", value->name);
|
||||
} else {
|
||||
printf("Test Failed: Key not found.\n");
|
||||
}
|
||||
remove_item(myTable, "key1");
|
||||
printf("remove key1.\n");
|
||||
test *value2 = find_value(myTable, input);
|
||||
if (value2) {
|
||||
printf("Test Passed: %s\n", value2->name);
|
||||
} else {
|
||||
printf("Test Failed: Key not found.\n");
|
||||
}
|
||||
add_item(myTable, "key1", test1);
|
||||
test *value4 = find_value(myTable, "key1");
|
||||
if (value4) {
|
||||
printf("Test Passed: %s\n", value4->name);
|
||||
} else {
|
||||
printf("Test Failed: Key not found.\n");
|
||||
}
|
||||
test *value3 = find_value(myTable, "key2");
|
||||
if (value3) {
|
||||
printf("Test Passed: %s\n", value3->name);
|
||||
} else {
|
||||
printf("Test Failed: Key not found.\n");
|
||||
}
|
||||
free(test1);
|
||||
free(test2);
|
||||
free_table(myTable);
|
||||
return 0;
|
||||
}
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,144 @@
|
||||
#ifndef HASH_H
|
||||
#define HASH_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
#include <pthread.h>
|
||||
#define TABLE_SIZE 32 // the size of hash table
|
||||
#define MAX_KEY_LENGTH 32 // the maximum length of key
|
||||
typedef struct {
|
||||
char key[MAX_KEY_LENGTH];
|
||||
void *value;
|
||||
int is_deleted; // Flag to mark items as deleted
|
||||
} HashItem;
|
||||
|
||||
typedef struct {
|
||||
HashItem **items;
|
||||
pthread_mutex_t lock;
|
||||
} HashTable;
|
||||
|
||||
static inline unsigned long
|
||||
hash_function(const char *str) {
|
||||
unsigned long i = 0;
|
||||
for (int j = 0; str[j]; j++)
|
||||
i += str[j];
|
||||
return i % TABLE_SIZE;
|
||||
}
|
||||
|
||||
static inline HashTable
|
||||
*create_table() {
|
||||
HashTable *table = (HashTable *)malloc(sizeof(HashTable));
|
||||
if (!table) {
|
||||
fprintf(stderr, "Failed to allocate memory for hash table struct.\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
table->items = (HashItem **)malloc(sizeof(HashItem*) * TABLE_SIZE);
|
||||
if (!table->items) {
|
||||
fprintf(stderr, "Failed to allocate memory for items.\n");
|
||||
free(table); // Free the table if item allocation fails
|
||||
return NULL;
|
||||
}
|
||||
|
||||
pthread_mutex_init(&table->lock, NULL);
|
||||
|
||||
for (int i = 0; i < TABLE_SIZE; i++) {
|
||||
table->items[i] = NULL;
|
||||
}
|
||||
return table;
|
||||
}
|
||||
|
||||
static inline void
|
||||
add_item(HashTable *table, const char *key, void *value) {
|
||||
assert(table != NULL);
|
||||
assert(key != NULL);
|
||||
assert(value != NULL);
|
||||
|
||||
pthread_mutex_lock(&table->lock);
|
||||
|
||||
unsigned long index = hash_function(key);
|
||||
HashItem *item = malloc(sizeof(HashItem));
|
||||
if (!item) {
|
||||
fprintf(stderr, "Failed to allocate memory for a new item.\n");
|
||||
pthread_mutex_unlock(&table->lock);
|
||||
return;
|
||||
}
|
||||
strcpy(item->key, key);
|
||||
item->value = value;
|
||||
item->is_deleted = 0;
|
||||
|
||||
while (table->items[index] != NULL && !table->items[index]->is_deleted && strcmp(table->items[index]->key, key) != 0) {
|
||||
index = (index + 1) % TABLE_SIZE;
|
||||
}
|
||||
|
||||
free(table->items[index]); // Free the existing item if overwriting
|
||||
table->items[index] = item;
|
||||
|
||||
pthread_mutex_unlock(&table->lock);
|
||||
}
|
||||
|
||||
static inline void
|
||||
remove_item(HashTable *table, const char *key) {
|
||||
assert(table != NULL);
|
||||
assert(key != NULL);
|
||||
|
||||
pthread_mutex_lock(&table->lock);
|
||||
|
||||
unsigned long index = hash_function(key);
|
||||
while (table->items[index] != NULL && strcmp(table->items[index]->key, key) != 0) {
|
||||
index = (index + 1) % TABLE_SIZE;
|
||||
}
|
||||
|
||||
if (table->items[index] != NULL) {
|
||||
table->items[index]->is_deleted = 1; // Mark as deleted
|
||||
}
|
||||
|
||||
pthread_mutex_unlock(&table->lock);
|
||||
}
|
||||
|
||||
static inline void
|
||||
*find_value(HashTable *table, const char *key) {
|
||||
assert(table != NULL);
|
||||
assert(key != NULL);
|
||||
|
||||
pthread_mutex_lock(&table->lock);
|
||||
|
||||
unsigned long index = hash_function(key);
|
||||
int count = 0;
|
||||
while (table->items[index] != NULL && count < TABLE_SIZE && (table->items[index]->is_deleted || strcmp(table->items[index]->key, key) != 0)) {
|
||||
index = (index + 1) % TABLE_SIZE;
|
||||
count++;
|
||||
}
|
||||
|
||||
if (table->items[index] == NULL || table->items[index]->is_deleted)
|
||||
{
|
||||
pthread_mutex_unlock(&table->lock);
|
||||
return NULL;
|
||||
}
|
||||
else {
|
||||
pthread_mutex_unlock(&table->lock);
|
||||
return table->items[index]->value;
|
||||
}
|
||||
}
|
||||
|
||||
static inline void
|
||||
free_table(HashTable *table) {
|
||||
assert(table != NULL);
|
||||
|
||||
pthread_mutex_lock(&table->lock);
|
||||
|
||||
for (int i = 0; i < TABLE_SIZE; i++) {
|
||||
if (table->items[i] != NULL) {
|
||||
free(table->items[i]); // Free each item
|
||||
}
|
||||
}
|
||||
free(table->items);
|
||||
free(table);
|
||||
|
||||
pthread_mutex_unlock(&table->lock);
|
||||
pthread_mutex_destroy(&table->lock);
|
||||
}
|
||||
|
||||
#endif
|
Binary file not shown.
@ -0,0 +1,46 @@
|
||||
{
|
||||
"active": true,
|
||||
"name": "cifar10_1",
|
||||
"path": "cifar10_wasm.so",
|
||||
"port": 10000,
|
||||
"relative-deadline-us": 78574,
|
||||
"argsize": 1,
|
||||
"http-req-headers": [],
|
||||
"http-req-content-type": "image/bmp",
|
||||
"http-req-size": 4096000,
|
||||
"http-resp-headers": [],
|
||||
"http-resp-size": 1024,
|
||||
"http-resp-content-type": "text/plain",
|
||||
"tail-module": true
|
||||
},
|
||||
|
||||
{
|
||||
"active": true,
|
||||
"name": "resize1",
|
||||
"path": "resize_wasm.so",
|
||||
"port": 10001,
|
||||
"relative-deadline-us": 78574,
|
||||
"argsize": 1,
|
||||
"http-req-headers": [],
|
||||
"http-req-content-type": "image/jpeg",
|
||||
"http-req-size": 1024000,
|
||||
"http-resp-headers": [],
|
||||
"http-resp-size": 1024000,
|
||||
"http-resp-content-type": "image/png"
|
||||
},
|
||||
|
||||
{
|
||||
"active": true,
|
||||
"name": "resize2",
|
||||
"path": "resize_wasm.so",
|
||||
"port": 10002,
|
||||
"relative-deadline-us": 78574,
|
||||
"argsize": 1,
|
||||
"http-req-headers": [],
|
||||
"http-req-content-type": "image/jpeg",
|
||||
"http-req-size": 1024000,
|
||||
"http-resp-headers": [],
|
||||
"http-resp-size": 1024000,
|
||||
"http-resp-content-type": "image/png"
|
||||
},
|
||||
|
@ -0,0 +1,16 @@
|
||||
{
|
||||
"active": true,
|
||||
"name": "tree",
|
||||
"path": "tree_wasm.so",
|
||||
"port": 10000,
|
||||
"expected-execution-us": 5000,
|
||||
"relative-deadline-us": 360000,
|
||||
"argsize": 1,
|
||||
"http-req-headers": [],
|
||||
"http-req-content-type": "text/plain",
|
||||
"http-req-size": 1024,
|
||||
"http-resp-headers": [],
|
||||
"http-resp-size": 1024,
|
||||
"http-resp-content-type": "text/plain"
|
||||
}
|
||||
|
Binary file not shown.
@ -0,0 +1,22 @@
|
||||
Runtime Environment:
|
||||
CPU Speed: 2400 MHz
|
||||
Processor Speed: 2400 MHz
|
||||
RLIMIT_DATA: Infinite
|
||||
RLIMIT_NOFILE: 1048576 (Increased from 8192)
|
||||
Core Count: 8
|
||||
Listener core ID: 1
|
||||
First Worker core ID: 2
|
||||
Worker core count: 6
|
||||
Scheduler Policy: EDF
|
||||
Sigalrm Policy: BROADCAST
|
||||
Preemption: Enabled
|
||||
Quantum: 5000 us
|
||||
Sandbox Performance Log: Disabled
|
||||
Starting listener thread
|
||||
Listener core thread: 7ffff7a006c0
|
||||
Starting 6 worker thread(s)
|
||||
C: 01, T: 0x7ffff7bfdd80, F: runtime_start_runtime_worker_threads>
|
||||
Sandboxing environment ready!
|
||||
|
||||
C: 01, T: 0x7ffff7bfdd80, F: module_new>
|
||||
Stack Size: 524288
|
Loading…
Reference in new issue