github.com/moontrade/wavm-go@v0.3.2-0.20220316110326-d229dd66ad65/worker/worker.c (about) 1 #include "worker.h" 2 3 #if defined(__APPLE__) || defined(__MACH__) 4 #include <mach/mach_time.h> 5 #endif 6 7 #ifndef unlikely 8 # if (defined(__GNUC__) || __has_builtin(__builtin_expect)) && !defined(__COVERITY__) 9 # define unlikely(cond) __builtin_expect(!!(cond), 0) 10 # else 11 # define unlikely(x) (!!(x)) 12 # endif 13 #endif /* unlikely */ 14 15 static __thread moontrade_worker_t* WORKER; 16 17 void moontrade_stub() {} 18 19 uint64_t moontrade_now(void) { 20 #if defined(_WIN32) || defined(_WIN64) 21 LARGE_INTEGER counter; 22 counter.QuadPart = 0; 23 QueryPerformanceCounter(&counter); 24 return counter.QuadPart; 25 #elif defined(__APPLE__) || defined(__MACH__) 26 return mach_absolute_time(); 27 #else 28 29 #if defined(__linux__) || defined(__gnu_linux__) 30 // static clockid_t posix_clockid = -1; 31 // if (unlikely(posix_clockid < 0)) 32 // posix_clockid = choice_monoclock(); 33 #elif defined(CLOCK_MONOTONIC) 34 #define posix_clockid CLOCK_MONOTONIC 35 #else 36 #define posix_clockid CLOCK_REALTIME 37 #endif 38 39 struct timespec ts; 40 if (unlikely(clock_gettime(CLOCK_MONOTONIC, &ts) != 0)) { 41 ts.tv_nsec = 0; 42 ts.tv_sec = 0; 43 } 44 return (uint64_t)ts.tv_sec * (uint64_t)1000000000 + (uint64_t)ts.tv_nsec; 45 #endif 46 } 47 48 // int64_t moontrade_now() { 49 // struct timespec t; 50 // clock_gettime(CLOCK_MONOTONIC, &t); 51 // return ((int64_t)t.tv_sec * (int64_t)1000000000) + (int64_t)t.tv_nsec; 52 // } 53 54 static inline int64_t now() { 55 struct timespec t; 56 clock_gettime(CLOCK_MONOTONIC, &t); 57 return ((int64_t)t.tv_sec * (int64_t)1000000000) + (int64_t)t.tv_nsec; 58 } 59 60 void moontrade_benchmark_stub(moontrade_worker_t* worker, int64_t n) { 61 WORKER = worker; 62 for (int64_t i = 0; i < n; i++) { 63 WORKER = worker; 64 wasm_trap_t* trap = ((wasm_func_call_t)worker->funcCall)(worker->store, worker->stub, NULL, NULL); 65 if (trap != NULL) { 66 67 } 68 } 69 } 70 71 static wasm_trap_t* do_stub_call(moontrade_worker_t* worker, 72 void* fn, 73 const wasm_val_t args[], 74 wasm_val_t results[]) { 75 WORKER = worker; 76 // worker->begin = now(); 77 wasm_trap_t* trap = ((wasm_func_call_t)fn)(worker->store, worker->stub, NULL, NULL); 78 // wasm_trap_t* trap = ((wasm_func_call_t)worker->funcCall)(worker->store, func, args, results); 79 // worker->end = now(); 80 return trap; 81 } 82 83 void moontrade_benchmark_stub_fn(moontrade_worker_t* worker, void* fn, int64_t n) { 84 WORKER = worker; 85 for (int64_t i = 0; i < n; i++) { 86 WORKER = worker; 87 // do_stub_call(worker, worker->stub, NULL, NULL); 88 ((wasm_func_call_t)fn)(worker->store, worker->stub, NULL, NULL); 89 } 90 } 91 92 wasm_trap_t* moontrade_start(moontrade_worker_t* worker) { 93 WORKER = worker; 94 worker->resumeAt = 0; 95 // worker->begin = now(); 96 wasm_trap_t* trap = ((wasm_func_call_t)worker->funcCall)(worker->store, worker->start, NULL, NULL); 97 // worker->end = now(); 98 printf("WASM moontrade_start: %llu ns\n", (long long unsigned)worker->end - (long long unsigned)worker->begin); 99 return trap; 100 } 101 102 wasm_trap_t* moontrade_resume(moontrade_worker_t* worker) { 103 WORKER = worker; 104 worker->resumeAt = 0; 105 // worker->begin = now(); 106 wasm_trap_t* trap = ((wasm_func_call_t)worker->funcCall)(worker->store, worker->resume, NULL, NULL); 107 // worker->end = now(); 108 printf("WASM moontrade_resume: %llu\n", (long long unsigned)WORKER->begin); 109 return trap; 110 } 111 112 void moontrade_call(uintptr_t arg0, uintptr_t arg1) { 113 //printf("moontrade_call_fuzzer\n"); 114 // Cast first arg to moontrade_call_t* 115 moontrade_call_t* call = (moontrade_call_t*)arg0; 116 WORKER = call->worker; 117 call->trap = ((wasm_func_call_t)call->thunk)(call->worker->store, call->func, *(call->args), *(call->results)); 118 } 119 120 //wasm_trap_t* moontrade_call(moontrade_worker_t* worker, 121 // const wasm_func_t* func, 122 // const wasm_val_t args[], 123 // wasm_val_t results[]) { 124 // WORKER = worker; 125 //// printf("moontrade_call\n"); 126 //// worker->begin = now(); 127 // wasm_trap_t* trap = ((wasm_func_call_t)worker->funcCall)(worker->store, func, args, results); 128 //// worker->end = now(); 129 // return trap; 130 //} 131 132 // fd_write 133 wasm_trap_t* moontrade_fd_write(const wasm_val_t args[], wasm_val_t results[]) { 134 struct moontrade_worker_t* worker = WORKER; 135 136 // printf("WASM fd_write: %llu\n", (uint64_t)WORKER->begin); 137 138 // Append to worker log buffer 139 return NULL; 140 } 141 142 // clock_time_get 143 wasm_trap_t* moontrade_clock_time_get(const wasm_val_t args[], wasm_val_t results[]) { 144 //printf("WASM clock_time_get\n"); 145 struct timespec now; 146 clock_gettime(CLOCK_REALTIME, &now); 147 //system_clock::time_point begin = system_clock::now(); 148 int64_t epoch = ((int64_t)now.tv_sec * (int64_t)1000000000) + (int64_t)now.tv_nsec; 149 150 // printf("WASM clock_time_get: %llu\n", (uint64_t)epoch); 151 return NULL; 152 } 153 154 // args_sizes_get 155 wasm_trap_t* moontrade_args_sizes_get(const wasm_val_t args[], wasm_val_t results[]) { 156 struct moontrade_worker_t* worker = WORKER; 157 // printf("WASM args_sizes_get_001\n"); 158 return NULL; 159 } 160 161 // args_get 162 wasm_trap_t* moontrade_args_get(const wasm_val_t args[], wasm_val_t results[]) { 163 struct moontrade_worker_t* worker = WORKER; 164 // printf("WASM args_get_001: %i\n", args[0].i32); 165 return NULL; 166 } 167 168 // set_timeout 169 wasm_trap_t* moontrade_set_timeout(const wasm_val_t args[], wasm_val_t results[]) { 170 struct moontrade_worker_t* worker = WORKER; 171 172 // Calculate resumeAt 173 int64_t resumeAt = now() + (int64_t)args[0].i64; 174 175 // Update worker resumeAt if earlier 176 int64_t currentResumeAt = worker->resumeAt; 177 if (currentResumeAt == 0 || resumeAt < currentResumeAt) { 178 worker->resumeAt = resumeAt; 179 180 if (currentResumeAt > 0) { 181 if (worker->resumeAtStackLen >= 128) { 182 return NULL; 183 } 184 // Push into sleep buffer 185 if (worker->resumeAtStack == NULL) { 186 worker->resumeAtStack = (int64_t*)malloc(sizeof(int64_t)*8); 187 } 188 } 189 } 190 191 printf("WASM set_timeout: worker: %llu nanos: %llu resumeAt: %llu\n", (long long unsigned)worker->id, (long long unsigned)args[0].i64, (long long unsigned)resumeAt); 192 // moontradeSetTimeout((void*)WORKER, (int64_t)args[0].i64); 193 return NULL; 194 }