item : map.entrySet()) {
222 | subSpan.addExtraData(item.getKey(), item.getValue());
223 | }
224 | }
225 | completeAndLogSpan(subSpan);
226 | }
227 |
228 |
229 | protected void completeAndLogSpan(Span span) {
230 | // Complete the span.
231 | if (span.hasComplete()) {
232 | System.err.println(
233 | "USAGE ERROR - An attempt was made to complete a span that was already completed. This call will be ignored. "
234 | + "wingtips_usage_error=true, already_completed_span=true, trace_id={}, span_id={}"
235 | );
236 | return;
237 | }
238 | else
239 | span.complete();
240 |
241 | // Notify listeners.
242 | notifySpanCompleted(span);
243 | }
244 |
245 |
246 | protected boolean isNextRootSpanSampleable() {
247 | return rootSpanSamplingStrategy.isNextRootSpanSampleable();
248 | }
249 |
250 |
251 | public void addSpanLifecycleListener(SpanLifecycleListener listener) {
252 | if (listener != null)
253 | this.spanLifecycleListeners.add(listener);
254 | }
255 |
256 |
257 | protected void notifySpanStarted(Span span) {
258 | for (SpanLifecycleListener tll : spanLifecycleListeners) {
259 | tll.spanStarted(span);
260 | }
261 | }
262 |
263 |
264 | private void notifyIfSpanSampled(Span span) {
265 | if (span.sampleable()) {
266 | for (SpanLifecycleListener tll : spanLifecycleListeners) {
267 | tll.spanSampled(span);
268 | }
269 | }
270 | }
271 |
272 |
273 | protected void notifySpanCompleted(Span span) {
274 | for (SpanLifecycleListener tll : spanLifecycleListeners) {
275 | tll.spanCompleted(span);
276 | }
277 | }
278 |
279 |
280 | }
281 |
--------------------------------------------------------------------------------
/javaagent-demo/src/main/java/me/geek01/javaagent/Wildcard.java:
--------------------------------------------------------------------------------
1 | package me.geek01.javaagent;
2 |
3 |
4 | /**
5 | * Checks whether a string or path matches a given wildcard pattern.
6 | * Possible patterns allow to match single characters ('?') or any count of
7 | * characters ('*'). Wildcard characters can be escaped (by an '\').
8 | * When matching path, deep tree wildcard also can be used ('**').
9 | *
10 | * This method uses recursive matching, as in linux or windows. regexp works the same.
11 | * This method is very fast, comparing to similar implementations.
12 | */
13 | public class Wildcard {
14 |
15 | /**
16 | * Checks whether a string matches a given wildcard pattern.
17 | *
18 | * @param string input string
19 | * @param pattern pattern to match
20 | * @return true
if string matches the pattern, otherwise false
21 | */
22 | public static boolean match(CharSequence string, CharSequence pattern) {
23 | return match(string, pattern, 0, 0);
24 | }
25 |
26 | /**
27 | * Checks if two strings are equals or if they {@link #match(CharSequence, CharSequence)}.
28 | * Useful for cases when matching a lot of equal strings and speed is important.
29 | */
30 | public static boolean equalsOrMatch(CharSequence string, CharSequence pattern) {
31 | if (string.equals(pattern)) {
32 | return true;
33 | }
34 | return match(string, pattern, 0, 0);
35 | }
36 |
37 | /**
38 | * Internal matching recursive function.
39 | */
40 | private static boolean match(CharSequence string, CharSequence pattern, int sNdx, int pNdx) {
41 | int pLen = pattern.length();
42 | if (pLen == 1) {
43 | if (pattern.charAt(0) == '*') { // speed-up
44 | return true;
45 | }
46 | }
47 | int sLen = string.length();
48 | boolean nextIsNotWildcard = false;
49 |
50 | while (true) {
51 |
52 | // check if end of string and/or pattern occurred
53 | if ((sNdx >= sLen)) { // end of string still may have pending '*' in pattern
54 | while ((pNdx < pLen) && (pattern.charAt(pNdx) == '*')) {
55 | pNdx++;
56 | }
57 | return pNdx >= pLen;
58 | }
59 | if (pNdx >= pLen) { // end of pattern, but not end of the string
60 | return false;
61 | }
62 | char p = pattern.charAt(pNdx); // pattern char
63 |
64 | // perform logic
65 | if (!nextIsNotWildcard) {
66 |
67 | if (p == '\\') {
68 | pNdx++;
69 | nextIsNotWildcard = true;
70 | continue;
71 | }
72 | if (p == '?') {
73 | sNdx++;
74 | pNdx++;
75 | continue;
76 | }
77 | if (p == '*') {
78 | char pNext = 0; // next pattern char
79 | if (pNdx + 1 < pLen) {
80 | pNext = pattern.charAt(pNdx + 1);
81 | }
82 | if (pNext == '*') { // double '*' have the same effect as one '*'
83 | pNdx++;
84 | continue;
85 | }
86 | int i;
87 | pNdx++;
88 |
89 | // find recursively if there is any substring from the end of the
90 | // line that matches the rest of the pattern !!!
91 | for (i = string.length(); i >= sNdx; i--) {
92 | if (match(string, pattern, i, pNdx)) {
93 | return true;
94 | }
95 | }
96 | return false;
97 | }
98 | } else {
99 | nextIsNotWildcard = false;
100 | }
101 |
102 | // check if pattern char and string char are equals
103 | if (p != string.charAt(sNdx)) {
104 | return false;
105 | }
106 |
107 | // everything matches for now, continue
108 | sNdx++;
109 | pNdx++;
110 | }
111 | }
112 |
113 |
114 | // ---------------------------------------------------------------- utilities
115 |
116 | /**
117 | * Matches string to at least one pattern.
118 | * Returns index of matched pattern, or -1
otherwise.
119 | *
120 | * @see #match(CharSequence, CharSequence)
121 | */
122 | public static int matchOne(String src, String[] patterns) {
123 | for (int i = 0; i < patterns.length; i++) {
124 | if (match(src, patterns[i])) {
125 | return i;
126 | }
127 | }
128 | return -1;
129 | }
130 | }
131 |
--------------------------------------------------------------------------------
/jvm/bytecode/Hello.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/arthur-zhang/geek01/776923f0ec91ec235bbe08cfa398cff33c96daa1/jvm/bytecode/Hello.class
--------------------------------------------------------------------------------
/jvm/bytecode/Hello.java:
--------------------------------------------------------------------------------
1 | public class Hello {
2 | public static void main(String[] args) {
3 | System.out.println("Hello, World");
4 | }
5 | }
--------------------------------------------------------------------------------
/zero_copy/zero_copy_basic/CMakeLists.txt:
--------------------------------------------------------------------------------
1 | cmake_minimum_required(VERSION 3.13)
2 | project(zero_copy C)
3 |
4 | set(CMAKE_C_STANDARD 99)
5 |
6 | add_executable(zero_copy main.c)
--------------------------------------------------------------------------------
/zero_copy/zero_copy_basic/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Title
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/zero_copy/zero_copy_basic/main.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 | #include
5 | #include
6 | #include
7 | #include
8 | #include
9 | #include
10 | #include
11 | #include
12 | #include
13 | #include /* ANSI C header file */
14 | #include
15 | #include
16 | #include
17 | #include
18 | #include
19 |
20 | #define LISTENQ 1024 /* 2nd argument to listen() */
21 | #define BUFFER_SIZE 64 /* size of buffer used by setbuf */
22 |
23 | void
24 | err_quit(const char *fmt) {
25 | printf("%s", fmt);
26 | exit(1);
27 | }
28 |
29 | int main() {
30 | printf("Hello, World!\n");
31 |
32 | struct sockaddr_in serv_addr;
33 |
34 | int listen_fd = 0;
35 | // socket(int domain, int type, int protocol);
36 | if ((listen_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
37 | exit(1);
38 | }
39 | bzero(&serv_addr, sizeof(serv_addr));
40 |
41 | serv_addr.sin_family = AF_INET;
42 | serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
43 | serv_addr.sin_port = htons(34567);
44 |
45 | int optval = 1;
46 | if (setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &optval,
47 | sizeof(optval)) == -1) {
48 | close(listen_fd);
49 | err_quit("set socket opt failed");
50 | }
51 | if (bind(listen_fd, &serv_addr, sizeof(serv_addr)) < 0)
52 | err_quit("bind failed");
53 |
54 | if (listen(listen_fd, LISTENQ) < 0)
55 | err_quit("listen failed");
56 |
57 | struct sockaddr_in client_addr;
58 |
59 | int connect_fd = 0;
60 | int fd = open("/Users/arthur/CLionProjects/zero_copy/index.html", O_RDWR);
61 |
62 | if (fd < 0) err_quit("error open file");
63 |
64 |
65 | for (;;) {
66 | if (lseek(fd, 0, SEEK_SET) < 0) err_quit("error seek file");
67 |
68 | connect_fd = accept(listen_fd, &serv_addr, &client_addr);
69 | if (connect_fd < 0) err_quit("accept failed");
70 |
71 | int n = 0;
72 | char buf[BUFFER_SIZE];
73 | while ((n = read(fd, buf, BUFFER_SIZE)) > 0) {
74 | write(connect_fd, buf, n);
75 | }
76 | close(connect_fd);
77 | }
78 | }
79 |
80 |
81 |
--------------------------------------------------------------------------------
/zero_copy/zerocopy_sendfile/CMakeLists.txt:
--------------------------------------------------------------------------------
1 | cmake_minimum_required(VERSION 3.13)
2 | project(zerocopy_sendfile C)
3 |
4 | set(CMAKE_C_STANDARD 99)
5 |
6 | add_executable(zerocopy_sendfile main.c)
--------------------------------------------------------------------------------
/zero_copy/zerocopy_sendfile/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Title
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/zero_copy/zerocopy_sendfile/main.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 | #include
5 | #include
6 | #include
7 | #include
8 | #include
9 | #include
10 | #include
11 | #include
12 | #include
13 | #include /* ANSI C header file */
14 | #include
15 | #include
16 | #include
17 | #include
18 | #include
19 | #include
20 |
21 | #define LISTENQ 1024 /* 2nd argument to listen() */
22 | #define BUFER_SIZE 64 /* size of buffer used by setbuf */
23 |
24 | void
25 | err_quit(const char *fmt) {
26 | printf("%s", fmt);
27 | exit(1);
28 | }
29 |
30 | int main() {
31 | printf("Hello, World!\n");
32 |
33 | struct sockaddr_in serv_addr;
34 |
35 | int listen_fd = 0;
36 | // socket(int domain, int type, int protocol);
37 | if ((listen_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
38 | exit(1);
39 | }
40 | bzero(&serv_addr, sizeof(serv_addr));
41 |
42 | serv_addr.sin_family = AF_INET;
43 | serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
44 | serv_addr.sin_port = htons(34567);
45 |
46 | int optval = 1;
47 | if (setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &optval,
48 | sizeof(optval)) == -1) {
49 | close(listen_fd);
50 | err_quit("set socket opt failed");
51 | }
52 | if (bind(listen_fd, &serv_addr, sizeof(serv_addr)) < 0)
53 | err_quit("bind failed");
54 |
55 | if (listen(listen_fd, LISTENQ) < 0)
56 | err_quit("listen failed");
57 |
58 | struct sockaddr_in client_addr;
59 |
60 | int connect_fd = 0;
61 | int fd = open("./index.html", O_RDWR);
62 |
63 | if (fd < 0) err_quit("error open file");
64 |
65 |
66 | for (;;) {
67 |
68 | connect_fd = accept(listen_fd, &serv_addr, &client_addr);
69 | if (connect_fd < 0) err_quit("accept failed");
70 |
71 | struct stat stat_buf;
72 | fstat(fd, &stat_buf);
73 |
74 | off_t offset = 0;
75 |
76 | int cnt = 0;
77 | if ((cnt = sendfile(connect_fd, fd, &offset, stat_buf.st_size)) < 0) {
78 | err_quit("send file failed");
79 | }
80 | close(connect_fd);
81 | }
82 | }
83 |
84 |
85 |
86 |
--------------------------------------------------------------------------------