aboutsummaryrefslogtreecommitdiff
path: root/src/tools/perf_replies.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/tools/perf_replies.c')
-rw-r--r--src/tools/perf_replies.c132
1 files changed, 104 insertions, 28 deletions
diff --git a/src/tools/perf_replies.c b/src/tools/perf_replies.c
index 24df534f..9f2a5305 100644
--- a/src/tools/perf_replies.c
+++ b/src/tools/perf_replies.c
@@ -39,6 +39,7 @@
39#include "mhd_options.h" 39#include "mhd_options.h"
40#include "microhttpd.h" 40#include "microhttpd.h"
41#include "mhd_tool_str_to_uint.h" 41#include "mhd_tool_str_to_uint.h"
42#include "mhd_tool_get_cpu_count.h"
42 43
43#if defined(MHD_REAL_CPU_COUNT) 44#if defined(MHD_REAL_CPU_COUNT)
44#if MHD_REAL_CPU_COUNT == 0 45#if MHD_REAL_CPU_COUNT == 0
@@ -165,28 +166,37 @@ get_cmd_out_as_number (const char *cmd)
165static unsigned int 166static unsigned int
166detect_cpu_core_count (void) 167detect_cpu_core_count (void)
167{ 168{
168 int sys_cpu_count = -1; 169 int sys_cpu_count;
169#if ! defined(_WIN32) || defined(__CYGWIN__) 170 sys_cpu_count = mhd_tool_get_system_cpu_count ();
170 sys_cpu_count = get_cmd_out_as_number ("nproc 2>/dev/null");
171#endif /* ! _WIN32) || __CYGWIN__ */
172#ifdef _WIN32
173 if (0 >= sys_cpu_count)
174 sys_cpu_count = get_cmd_out_as_number ("echo %NUMBER_OF_PROCESSORS%");
175#endif /* _WIN32 */
176 if (0 >= sys_cpu_count) 171 if (0 >= sys_cpu_count)
177 { 172 {
178 fprintf (stderr, "Failed to detect the number of available CPU cores.\n"); 173 int proc_cpu_count;
174 fprintf (stderr, "Failed to detect the number of logical CPU cores "
175 "available on the system.\n");
176 proc_cpu_count = mhd_tool_get_proc_cpu_count ();
177 if (0 < proc_cpu_count)
178 {
179 fprintf (stderr, "The number of CPU cores available for this process "
180 "is used as a fallback.\n");
181 sys_cpu_count = proc_cpu_count;
182 }
179#ifdef MHD_REAL_CPU_COUNT 183#ifdef MHD_REAL_CPU_COUNT
180 fprintf (stderr, "Hardcoded number is used as a fallback.\n"); 184 if (0 >= sys_cpu_count)
181 sys_cpu_count = MHD_REAL_CPU_COUNT; 185 {
186 fprintf (stderr, "configure-detected hardcoded number is used "
187 "as a fallback.\n");
188 sys_cpu_count = MHD_REAL_CPU_COUNT;
189 }
182#endif 190#endif
183 if (0 >= sys_cpu_count) 191 if (0 >= sys_cpu_count)
184 sys_cpu_count = 1; 192 sys_cpu_count = 1;
185 printf ("Assuming %d CPU cores.\n", sys_cpu_count); 193 printf ("Assuming %d logical CPU core%s on this system.\n", sys_cpu_count,
194 (1 == sys_cpu_count) ? "" : "s");
186 } 195 }
187 else 196 else
188 { 197 {
189 printf ("Detected %d CPU cores.\n", sys_cpu_count); 198 printf ("Detected %d logical CPU core%s on this system.\n", sys_cpu_count,
199 (1 == sys_cpu_count) ? "" : "s");
190 } 200 }
191 return (unsigned int) sys_cpu_count; 201 return (unsigned int) sys_cpu_count;
192} 202}
@@ -202,20 +212,84 @@ get_cpu_core_count (void)
202} 212}
203 213
204 214
215static unsigned int
216detect_process_cpu_core_count (void)
217{
218 unsigned int num_proc_cpu_cores;
219 unsigned int sys_cpu_cores;
220 int res;
221
222 sys_cpu_cores = get_cpu_core_count ();
223 res = mhd_tool_get_proc_cpu_count ();
224 if (0 > res)
225 {
226 fprintf (stderr, "Cannot detect the number of logical CPU cores available "
227 "for this process.\n");
228 if (1 != sys_cpu_cores)
229 printf ("Assuming all %u system logical CPU cores are available to run "
230 "threads of this process.\n", sys_cpu_cores);
231 else
232 printf ("Assuming single logical CPU core available for this process.\n");
233 num_proc_cpu_cores = sys_cpu_cores;
234 }
235 else
236 {
237 printf ("Detected %d logical CPU core%s available to run threads "
238 "of this process.\n", res, (1 == res) ? "" : "s");
239 num_proc_cpu_cores = (unsigned int) res;
240 }
241 if (num_proc_cpu_cores > sys_cpu_cores)
242 {
243 fprintf (stderr, "WARNING: Detected number of CPU cores available "
244 "for this process (%u) is larger than detected number "
245 "of CPU cores on the system (%u).\n",
246 num_proc_cpu_cores, sys_cpu_cores);
247 num_proc_cpu_cores = sys_cpu_cores;
248 fprintf (stderr, "Using %u as the number of logical CPU cores available "
249 "for this process.\n", num_proc_cpu_cores);
250 }
251 return num_proc_cpu_cores;
252}
253
254
255static unsigned int
256get_process_cpu_core_count (void)
257{
258 static unsigned int proc_num_cpu_cores = 0;
259 if (0 == proc_num_cpu_cores)
260 proc_num_cpu_cores = detect_process_cpu_core_count ();
261 return proc_num_cpu_cores;
262}
263
264
205static unsigned int num_threads = 0; 265static unsigned int num_threads = 0;
206 266
207static unsigned int 267static unsigned int
208get_num_threads (void) 268get_num_threads (void)
209{ 269{
210 static const unsigned int max_threads = 32; 270 static const unsigned int max_threads = 32;
271 if (0 < num_threads)
272 return num_threads;
273
274 num_threads = get_cpu_core_count () / 2;
211 if (0 == num_threads) 275 if (0 == num_threads)
276 num_threads = 1;
277 else
212 { 278 {
213 num_threads = get_cpu_core_count () / 2; 279 unsigned int num_proc_cpus;
214 if (0 == num_threads) 280 num_proc_cpus = get_process_cpu_core_count ();
215 num_threads = 1; 281 if (num_proc_cpus >= num_threads)
216 else 282 {
217 printf ("Using half of all available CPU cores, assuming the other half " 283 printf ("Using half of all available CPU cores, assuming the other half "
218 "is used by client / requests generator.\n"); 284 "is used by client / requests generator.\n");
285 }
286 else
287 {
288 printf ("Using all CPU cores available for this process as more than "
289 "half of CPU cores on this system are still available for use "
290 "by client / requests generator.\n");
291 num_threads = num_proc_cpus;
292 }
219 } 293 }
220 if (max_threads < num_threads) 294 if (max_threads < num_threads)
221 { 295 {
@@ -1059,9 +1133,10 @@ check_apply_param__all_cpus (void)
1059 if (! tool_params.all_cpus) 1133 if (! tool_params.all_cpus)
1060 return; 1134 return;
1061 1135
1062 num_threads = get_cpu_core_count (); 1136 num_threads = get_process_cpu_core_count ();
1063 printf ("Requested use of all available CPU cores for MHD threads.\n"); 1137 printf ("Requested use of all available CPU cores for MHD threads.\n");
1064 print_all_cores_used (); 1138 if (get_cpu_core_count () == num_threads)
1139 print_all_cores_used ();
1065} 1140}
1066 1141
1067 1142
@@ -1075,20 +1150,21 @@ check_apply_param__threads (void)
1075 return; 1150 return;
1076 1151
1077 num_threads = tool_params.threads; 1152 num_threads = tool_params.threads;
1153
1154 if (get_process_cpu_core_count () < num_threads)
1155 {
1156 fprintf (stderr, "WARNING: The requested number of threads (%u) is "
1157 "higher than the number of detected available CPU cores (%u).\n",
1158 num_threads, get_process_cpu_core_count ());
1159 fprintf (stderr, "This decreases the performance. "
1160 "Consider using fewer threads.\n");
1161 }
1078 if (get_cpu_core_count () == num_threads) 1162 if (get_cpu_core_count () == num_threads)
1079 { 1163 {
1080 printf ("The requested number of threads is equal to the number of " 1164 printf ("The requested number of threads is equal to the number of "
1081 "detected CPU cores.\n"); 1165 "detected CPU cores.\n");
1082 print_all_cores_used (); 1166 print_all_cores_used ();
1083 } 1167 }
1084 else if (get_cpu_core_count () < num_threads)
1085 {
1086 fprintf (stderr, "WARNING: The requested number of threads (%u) is "
1087 "higher than the number of detected CPU cores (%u).\n",
1088 num_threads, get_cpu_core_count ());
1089 fprintf (stderr, "This decreases the performance. "
1090 "Consider using fewer threads.\n");
1091 }
1092} 1168}
1093 1169
1094 1170
@@ -1228,7 +1304,7 @@ static struct MHD_Response *resp_single = NULL;
1228 The system will keep it in cache. */ 1304 The system will keep it in cache. */
1229static const char tiny_body[] = "Hi!"; 1305static const char tiny_body[] = "Hi!";
1230static char *body_dyn = NULL; /* Non-static body data */ 1306static char *body_dyn = NULL; /* Non-static body data */
1231size_t body_dyn_size; 1307static size_t body_dyn_size;
1232 1308
1233/* Non-zero - success, zero - failure */ 1309/* Non-zero - success, zero - failure */
1234static int 1310static int