Skip to content

Commit d44b383

Browse files
committed
snooper: Add testing "app" for task_work-based demo
Signed-off-by: Andrii Nakryiko <[email protected]>
1 parent 8f9cb1e commit d44b383

File tree

5 files changed

+164
-3
lines changed

5 files changed

+164
-3
lines changed

examples/c/.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,7 @@
1616
/lsm
1717
/cmake-build-debug/
1818
/cmake-build-release/
19-
/snooper
2019
compile_commands.json
20+
/app
21+
/libapp.so
22+
/snooper

examples/c/Makefile

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,12 @@ $(call allow-override,CC,$(CROSS_COMPILE)cc)
7070
$(call allow-override,LD,$(CROSS_COMPILE)ld)
7171

7272
.PHONY: all
73-
all: $(APPS)
73+
all: $(APPS) app
7474

7575
.PHONY: clean
7676
clean:
7777
$(call msg,CLEAN)
78-
$(Q)rm -rf $(OUTPUT) $(APPS)
78+
$(Q)rm -rf $(OUTPUT) $(APPS) app libapp.so
7979

8080
$(OUTPUT) $(OUTPUT)/libbpf $(BPFTOOL_OUTPUT):
8181
$(call msg,MKDIR,$@)
@@ -136,3 +136,12 @@ $(APPS): %: $(OUTPUT)/%.o $(LIBBPF_OBJ) | $(OUTPUT)
136136

137137
# keep intermediate (.skel.h, .bpf.o, etc) targets
138138
.SECONDARY:
139+
140+
# Build target app and its shared library
141+
libapp.so: app_lib.c app_lib.h
142+
$(call msg,SHLIB,$@)
143+
$(Q)$(CC) $(CFLAGS) -shared -fPIC -o $@ app_lib.c
144+
145+
app: app.c app_lib.h libapp.so
146+
$(call msg,BINARY,$@)
147+
$(Q)$(CC) $(CFLAGS) -o $@ app.c -L. -lapp -Wl,-rpath,'$$ORIGIN' -lpthread

examples/c/app.c

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
#define _GNU_SOURCE
2+
#include <stdio.h>
3+
#include <unistd.h>
4+
#include <sys/types.h>
5+
#include <sys/mman.h>
6+
#include <sys/stat.h>
7+
#include <fcntl.h>
8+
#include <errno.h>
9+
#include <string.h>
10+
#include <stdlib.h>
11+
#include <dlfcn.h>
12+
#include <elf.h>
13+
#include <pthread.h>
14+
#include "app_lib.h"
15+
16+
static __thread int tls_dont_care; /* just to avoid zero offsets everywhere else */
17+
18+
__thread int tls_exec;
19+
extern __thread int tls_shared;
20+
21+
static __thread int tls_local_exec;
22+
23+
int __attribute__((weak)) get_tls_exec(void)
24+
{
25+
return tls_exec;
26+
}
27+
28+
int __attribute__((weak)) get_tls_shared(void)
29+
{
30+
return tls_shared;
31+
}
32+
33+
int __attribute__((weak)) get_tls_local_exec(void)
34+
{
35+
return tls_local_exec;
36+
}
37+
38+
/* Forward declarations for recursive functions */
39+
void func_a(int depth);
40+
void func_b(int depth);
41+
void func_c(int depth);
42+
43+
static __always_inline void func_mux(int depth)
44+
{
45+
if (depth <= 0)
46+
return;
47+
48+
switch (rand() % 3) {
49+
case 0: func_a(depth - 1); break;
50+
case 1: func_b(depth - 1); break;
51+
case 2: func_c(depth - 1); break;
52+
}
53+
}
54+
55+
void func_a(int depth)
56+
{
57+
volatile char stack_space[120];
58+
stack_space[119] = 'a';
59+
stack_space[0] += 1;
60+
61+
if (depth <= 0)
62+
return;
63+
64+
func_mux(depth - 1);
65+
}
66+
67+
void func_b(int depth)
68+
{
69+
volatile char stack_space[350];
70+
stack_space[349] = 'b';
71+
stack_space[0] += 1;
72+
73+
if (depth <= 0)
74+
return;
75+
76+
func_mux(depth - 1);
77+
}
78+
79+
void func_c(int depth)
80+
{
81+
volatile char stack_space[800];
82+
stack_space[799] = 'c';
83+
stack_space[0] += 1;
84+
85+
if (depth <= 0)
86+
return;
87+
88+
func_mux(depth - 1);
89+
}
90+
91+
static void *thread_func(void *arg)
92+
{
93+
time_t last_print = 0;
94+
(void)arg;
95+
96+
pthread_setname_np(pthread_self(), "app_thread");
97+
98+
while (1) {
99+
time_t now;
100+
101+
func_mux(10);
102+
103+
now = time(NULL);
104+
if (now > last_print) {
105+
tls_exec += 4;
106+
tls_shared += 8;
107+
tls_local_exec += 16;
108+
bump_tls_local_shared();
109+
bump_tls_local_shared();
110+
111+
printf("Hello from thread (exec=%d, shared=%d, local_exec=%d, local_shared=%d)!\n",
112+
get_tls_exec(), get_tls_shared(), get_tls_local_exec(), get_tls_local_shared());
113+
last_print = now;
114+
}
115+
}
116+
117+
return NULL;
118+
}
119+
120+
int main() {
121+
pthread_t thread;
122+
123+
pthread_create(&thread, NULL, thread_func, NULL);
124+
125+
while (1) {
126+
tls_dont_care += 1;
127+
tls_exec += 2;
128+
tls_shared += 4;
129+
tls_local_exec += 8;
130+
bump_tls_local_shared();
131+
132+
printf("Hello from app (exec=%d, shared=%d, local_exec=%d, local_shared=%d)!\n",
133+
get_tls_exec(), get_tls_shared(), get_tls_local_exec(), get_tls_local_shared());
134+
sleep(1);
135+
}
136+
137+
return 0;
138+
}

examples/c/app_lib.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
__thread int tls_shared;
2+
static __thread int tls_local_shared;
3+
4+
int get_tls_local_shared(void) { return tls_local_shared; }
5+
void bump_tls_local_shared(void) { tls_local_shared += 16; }

examples/c/app_lib.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#ifndef APP_LIB_H
2+
#define APP_LIB_H
3+
4+
int get_tls_local_shared(void);
5+
void bump_tls_local_shared(void);
6+
7+
#endif /* APP_LIB_H */

0 commit comments

Comments
 (0)