/src/fluent-bit/lib/monkey/mk_server/mk_kernel.c
Line | Count | Source |
1 | | /*-*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | |
3 | | /* Monkey HTTP Server |
4 | | * ================== |
5 | | * Copyright 2001-2017 Eduardo Silva <eduardo@monkey.io> |
6 | | * |
7 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
8 | | * you may not use this file except in compliance with the License. |
9 | | * You may obtain a copy of the License at |
10 | | * |
11 | | * http://www.apache.org/licenses/LICENSE-2.0 |
12 | | * |
13 | | * Unless required by applicable law or agreed to in writing, software |
14 | | * distributed under the License is distributed on an "AS IS" BASIS, |
15 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | | * See the License for the specific language governing permissions and |
17 | | * limitations under the License. |
18 | | */ |
19 | | |
20 | | #include <monkey/monkey.h> |
21 | | #include <monkey/mk_core.h> |
22 | | #include <monkey/mk_kernel.h> |
23 | | #include <monkey/mk_utils.h> |
24 | | #include <monkey/mk_server.h> |
25 | | #include <monkey/mk_scheduler.h> |
26 | | |
27 | | #include <ctype.h> |
28 | | |
29 | | #ifndef _WIN32 |
30 | | #include <sys/utsname.h> |
31 | | |
32 | | int mk_kernel_version() |
33 | 0 | { |
34 | 0 | int a, b, c; |
35 | 0 | int len; |
36 | 0 | int pos; |
37 | 0 | char *p, *t; |
38 | 0 | char *tmp; |
39 | 0 | struct utsname uts; |
40 | |
|
41 | 0 | if (uname(&uts) == -1) { |
42 | 0 | mk_libc_error("uname"); |
43 | 0 | } |
44 | 0 | len = strlen(uts.release); |
45 | | |
46 | | /* Fixme: this don't support Linux Kernel 10.x.x :P */ |
47 | 0 | a = (*uts.release - '0'); |
48 | | |
49 | | /* Second number */ |
50 | 0 | p = (uts.release) + 2; |
51 | 0 | pos = mk_string_char_search(p, '.', len - 2); |
52 | 0 | if (pos <= 0) { |
53 | | /* Some Debian systems uses a different notation, e.g: 3.14-2-amd64 */ |
54 | 0 | pos = mk_string_char_search(p, '-', len - 2); |
55 | 0 | if (pos <= 0) { |
56 | 0 | return -1; |
57 | 0 | } |
58 | 0 | } |
59 | | |
60 | 0 | tmp = mk_string_copy_substr(p, 0, pos); |
61 | 0 | if (!tmp) { |
62 | 0 | return -1; |
63 | 0 | } |
64 | 0 | b = atoi(tmp); |
65 | 0 | mk_mem_free(tmp); |
66 | | |
67 | | /* Last number (it needs filtering) */ |
68 | 0 | t = p = p + pos + 1; |
69 | 0 | do { |
70 | 0 | t++; |
71 | 0 | } while (isdigit(*t)); |
72 | |
|
73 | 0 | tmp = mk_string_copy_substr(p, 0, t - p); |
74 | 0 | if (!tmp) { |
75 | 0 | return -1; |
76 | 0 | } |
77 | 0 | c = atoi(tmp); |
78 | 0 | mk_mem_free(tmp); |
79 | |
|
80 | 0 | MK_TRACE("Kernel detected: %i.%i.%i", a, b, c); |
81 | 0 | return MK_KERNEL_VERSION(a, b, c); |
82 | 0 | } |
83 | | |
84 | | /* Detect specific Linux Kernel features that we may use */ |
85 | | int mk_kernel_features(int version) |
86 | 0 | { |
87 | 0 | int flags = 0; |
88 | | |
89 | | /* |
90 | | * TCP Auto Corking (disabled by #175) |
91 | | * ----------------------------------- |
92 | | * I found that running some benchmarks on Linux 3.16 with |
93 | | * tcp_autocorking enabled, it lead to lower performance, looks like |
94 | | * a manual cork fits better for our needs. |
95 | | * |
96 | | * I think there is something wrong that we need to clarify, by now |
97 | | * I've logged the following issue: |
98 | | * |
99 | | * https://github.com/monkey/monkey/issues/175 |
100 | | * |
101 | | if (mk_kernel_runver >= MK_KERNEL_VERSION(3, 14, 0) && |
102 | | mk_socket_tcp_autocorking() == MK_TRUE) { |
103 | | flags |= MK_KERNEL_TCP_AUTOCORKING; |
104 | | } |
105 | | */ |
106 | | |
107 | | /* SO_REUSEPORT */ |
108 | 0 | if (version >= MK_KERNEL_VERSION(3, 9, 0)) { |
109 | 0 | flags |= MK_KERNEL_SO_REUSEPORT; |
110 | 0 | } |
111 | | |
112 | | /* TCP_FASTOPEN */ |
113 | 0 | if (version >= MK_KERNEL_VERSION(3, 7, 0)) { |
114 | 0 | flags |= MK_KERNEL_TCP_FASTOPEN; |
115 | 0 | } |
116 | |
|
117 | 0 | return flags; |
118 | 0 | } |
119 | | |
120 | | int mk_kernel_features_print(char *buffer, size_t size, |
121 | | struct mk_server *server) |
122 | 0 | { |
123 | 0 | int offset = 0; |
124 | 0 | int features = 0; |
125 | |
|
126 | 0 | if (server->kernel_features & MK_KERNEL_TCP_FASTOPEN) { |
127 | 0 | offset += snprintf(buffer, size - offset, "%s", "TCP_FASTOPEN "); |
128 | 0 | features++; |
129 | 0 | } |
130 | |
|
131 | 0 | if (server->kernel_features & MK_KERNEL_SO_REUSEPORT) { |
132 | 0 | if (server->scheduler_mode == MK_SCHEDULER_FAIR_BALANCING) { |
133 | 0 | offset += snprintf(buffer + offset, size - offset, |
134 | 0 | "%s!%s", ANSI_BOLD ANSI_RED, ANSI_RESET); |
135 | 0 | } |
136 | 0 | offset += snprintf(buffer + offset, size - offset, "%s", "SO_REUSEPORT "); |
137 | 0 | features++; |
138 | 0 | } |
139 | |
|
140 | 0 | if (server->kernel_features & MK_KERNEL_TCP_AUTOCORKING) { |
141 | 0 | snprintf(buffer + offset, size - offset, "%s", "TCP_AUTOCORKING "); |
142 | 0 | features++; |
143 | 0 | } |
144 | |
|
145 | 0 | return features; |
146 | 0 | } |
147 | | #else |
148 | | /* We still need to determine if this can be safely ignored or what do we need to do here */ |
149 | | |
150 | | int mk_kernel_version() |
151 | | { |
152 | | return 1; |
153 | | } |
154 | | |
155 | | int mk_kernel_features(int version) |
156 | | { |
157 | | return 0; |
158 | | } |
159 | | |
160 | | int mk_kernel_features_print(char* buffer, size_t size, |
161 | | struct mk_server* server) |
162 | | { |
163 | | return 0; |
164 | | } |
165 | | #endif |