Skip to content

Commit 18c986a

Browse files
committed
Fix build problems with API header and add shared_counter program
1 parent 91c0db3 commit 18c986a

File tree

5 files changed

+48
-11
lines changed

5 files changed

+48
-11
lines changed

compute.json

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@
6363
"filename": "/tmp/compute_gbcemu",
6464
"max_memory": 128,
6565
"max_request_memory": 64,
66-
"shared_memory": 2,
6766
"concurrency": 4,
6867
"storage": true
6968
},
@@ -124,11 +123,6 @@
124123
"uri": "https://filebin.varnish-software.com/tinykvm_programs/minify.tar.xz",
125124
"filename": "/tmp/compute_minify"
126125
},
127-
"rust": {
128-
"group": "compute",
129-
"uri": "https://filebin.varnish-software.com/tinykvm_programs/rustpng.tar.xz",
130-
"filename": "/tmp/compute_rust"
131-
},
132126
"nim_storage": {
133127
"group": "compute",
134128
"uri": "https://filebin.varnish-software.com/tinykvm_programs/storage_example.tar.xz",
@@ -141,6 +135,17 @@
141135
"filename": "/tmp/compute_nim_prefetch_task",
142136
"storage": true
143137
},
138+
"rust": {
139+
"group": "compute",
140+
"uri": "https://filebin.varnish-software.com/tinykvm_programs/rustpng.tar.xz",
141+
"filename": "/tmp/compute_rust"
142+
},
143+
"shared_counter": {
144+
"group": "compute",
145+
"uri": "https://filebin.varnish-software.com/tinykvm_programs/shared_counter.tar.xz",
146+
"filename": "/tmp/compute_shared_counter",
147+
"shared_memory": 2
148+
},
144149
"stable_diffusion": {
145150
"group": "compute",
146151
"uri": "https://filebin.varnish-software.com/tinykvm_programs/sdpp.tar.xz",

cpp/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ minimal/minimal
33
minimal/storage
44
collector/collector
55
collector/storage
6+
shared_counter/shared_counter
67
*.syms

cpp/shared_counter/build.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
g++ -O2 -static -std=c++20 shared_counter.cpp -o shared_counter
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include "../../kvm_api.h"
2+
static uint64_t* shared_counter = nullptr;
3+
4+
static void on_get(const char *url, const char*)
5+
{
6+
uint64_t counter = __sync_fetch_and_add(shared_counter, 1);
7+
8+
const char ct[] = "text/plain";
9+
char co[] = "Hell0 World, 000!";
10+
co[ 4] = '0' + (counter / 1000) % 10;
11+
co[13] = '0' + (counter / 100) % 10;
12+
co[14] = '0' + (counter / 10) % 10;
13+
co[15] = '0' + (counter / 1) % 10;
14+
backend_response(200, ct, sizeof(ct)-1, co, sizeof(co)-1);
15+
}
16+
17+
int main()
18+
{
19+
// Allocate a shared counter in shared memory
20+
shared_counter = SHM_ALLOC_TYPE(uint64_t);
21+
22+
set_backend_get(on_get);
23+
wait_for_requests();
24+
}

kvm_api.h

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ struct virtbuffer {
427427
* static size_t cont_len = 0;
428428
*
429429
* static void
430-
* modify_content(size_t n, struct virtbuffer buffers[n], size_t res)
430+
* modify_content(size_t n, struct virtbuffer buffers[], size_t res)
431431
* {
432432
* // Allocate a new zero-terminated buffer from input
433433
* char *new_buf = malloc(buffers[0].len + 1);
@@ -443,7 +443,7 @@ struct virtbuffer {
443443
* storage_return_nothing();
444444
* }
445445
* static void
446-
* get_content(size_t n, struct virtbuffer buffers[n], size_t res)
446+
* get_content(size_t n, struct virtbuffer buffers[], size_t res)
447447
* {
448448
* storage_return(cont, cont_len);
449449
* }
@@ -472,15 +472,15 @@ struct virtbuffer {
472472
* from the function. This allows the system to copy the data back to the
473473
* request program before the function has returned and deallocated the data.
474474
**/
475-
typedef void (*storage_func) (size_t n, struct virtbuffer[n], size_t res);
475+
typedef void (*storage_func) (size_t n, struct virtbuffer[], size_t res);
476476

477477
/* Returns true (1) if called from storage. */
478478
extern int
479479
sys_is_storage();
480480

481481
/* Transfer an array of buffers to storage, transfer output into @dst. */
482482
extern long
483-
storage_callv(storage_func, size_t n, const struct virtbuffer[n], void* dst, size_t);
483+
storage_callv(storage_func, size_t n, const struct virtbuffer[], void* dst, size_t);
484484

485485
/* Transfer an array to storage, transfer output into @dst. */
486486
static inline long
@@ -599,7 +599,13 @@ extern struct shared_memory_info shared_memory_area();
599599

600600
/* Allocate pointers to shared memory with given size and alignment. */
601601
#define SHM_ALLOC_BYTES(x) internal_shm_alloc(x, 8)
602-
#define SHM_ALLOC_TYPE(x) (typeof(x) *)internal_shm_alloc(sizeof(x), _Alignof(x))
602+
#ifdef __cplusplus
603+
#define SHM_ALLOC_TYPE(x) (x *)internal_shm_alloc(sizeof(x), alignof(x))
604+
#define SHM_ALLOC(x) (decltype(x) *)internal_shm_alloc(sizeof(x), alignof(x))
605+
#else
606+
#define SHM_ALLOC_TYPE(x) (x *)internal_shm_alloc(sizeof(x), _Alignof(x))
607+
#define SHM_ALLOC(x) (typeof(x) *)internal_shm_alloc(sizeof(x), _Alignof(x))
608+
#endif
603609
static inline void * internal_shm_alloc(size_t size, size_t align) {
604610
static struct shared_memory_info info;
605611
if (info.ptr == 0x0) {

0 commit comments

Comments
 (0)