-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathnumad.c
2672 lines (2523 loc) · 100 KB
/
numad.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
numad - NUMA Daemon to automatically bind processes to NUMA nodes
Copyright (C) 2012 Bill Gray ([email protected]), Red Hat Inc
numad is free software; you can redistribute it and/or modify it under the
terms of the GNU Lesser General Public License as published by the Free
Software Foundation; version 2.1.
numad is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
details.
You should find a copy of v2.1 of the GNU Lesser General Public License
somewhere on your Linux system; if not, write to the Free Software Foundation,
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
// Compile with: gcc -std=gnu99 -g -Wall -pthread -o numad numad.c -lrt -lm
#define _GNU_SOURCE
#include <assert.h>
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <limits.h>
#include <math.h>
#include <pthread.h>
#include <sched.h>
#include <signal.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <values.h>
#include <sys/ipc.h>
#include <sys/mman.h>
#include <sys/msg.h>
#include <sys/sem.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/syslog.h>
#include <sys/time.h>
#include <sys/types.h>
#include <asm/unistd.h>
#define VERSION_STRING "20160428"
#define VAR_RUN_FILE "/var/run/numad.pid"
#define VAR_LOG_FILE "/var/log/numad.log"
#define KILOBYTE (1024)
#define MEGABYTE (1024 * 1024)
#define FNAME_SIZE 192
#define BUF_SIZE 1024
#define BIG_BUF_SIZE 4096
// The ONE_HUNDRED factor is used to scale time and CPU usage units.
// Several CPU quantities are measured in percents of a CPU; and
// several time values are counted in hundreths of a second.
#define ONE_HUNDRED 100
#define MIN_INTERVAL 5
#define MAX_INTERVAL 15
#define CPU_THRESHOLD 50
#define MEMORY_THRESHOLD 300
#define DEFAULT_HTT_PERCENT 20
#define DEFAULT_THP_SCAN_SLEEP_MS 1000
#define DEFAULT_UTILIZATION_PERCENT 85
#define DEFAULT_MEMLOCALITY_PERCENT 90
#define CONVERT_DIGITS_TO_NUM(p, n) \
n = *p++ - '0'; \
while (isdigit(*p)) { \
n *= 10; \
n += (*p++ - '0'); \
}
int num_cpus = 0;
int num_nodes = 0;
int threads_per_core = 0;
uint64_t page_size_in_bytes = 0;
uint64_t huge_page_size_in_bytes = 0;
int min_interval = MIN_INTERVAL;
int max_interval = MAX_INTERVAL;
int htt_percent = DEFAULT_HTT_PERCENT;
int thp_scan_sleep_ms = DEFAULT_THP_SCAN_SLEEP_MS;
int target_utilization = DEFAULT_UTILIZATION_PERCENT;
int target_memlocality = DEFAULT_MEMLOCALITY_PERCENT;
int scan_all_processes = 1;
int keep_interleaved_memory = 0;
int use_inactive_file_cache = 1;
pthread_mutex_t pid_list_mutex;
pthread_mutex_t node_info_mutex;
long sum_CPUs_total = 0;
int requested_mbs = 0;
int requested_cpus = 0;
int got_sighup = 0;
int got_sigterm = 0;
int got_sigquit = 0;
void sig_handler(int signum) {
switch (signum) {
case SIGHUP: got_sighup = 1; break;
case SIGTERM: got_sigterm = 1; break;
case SIGQUIT: got_sigquit = 1; break;
}
}
FILE *log_fs = NULL;
int log_level = LOG_NOTICE;
void numad_log(int level, const char *fmt, ...) {
if (level > log_level) {
return;
// Logging levels (from sys/syslog.h)
// #define LOG_EMERG 0 /* system is unusable */
// #define LOG_ALERT 1 /* action must be taken immediately */
// #define LOG_CRIT 2 /* critical conditions */
// #define LOG_ERR 3 /* error conditions */
// #define LOG_WARNING 4 /* warning conditions */
// #define LOG_NOTICE 5 /* normal but significant condition */
// #define LOG_INFO 6 /* informational */
// #define LOG_DEBUG 7 /* debug-level messages */
}
char buf[BUF_SIZE];
time_t ts = time(NULL);
strncpy(buf, ctime(&ts), sizeof(buf));
char *p = &buf[strlen(buf) - 1];
*p++ = ':';
*p++ = ' ';
va_list ap;
va_start(ap, fmt);
vsnprintf(p, BUF_SIZE, fmt, ap);
va_end(ap);
fprintf(log_fs, "%s", buf);
fflush(log_fs);
}
void open_log_file() {
log_fs = fopen(VAR_LOG_FILE, "a");
if (log_fs == NULL) {
log_fs = stderr;
numad_log(LOG_ERR, "Cannot open numad log file (errno: %d) -- using stderr\n", errno);
}
}
void close_log_file() {
if (log_fs != NULL) {
if (log_fs != stderr) {
fclose(log_fs);
}
log_fs = NULL;
}
}
#define MSG_BODY_TEXT_SIZE 96
typedef struct msg_body {
long src_pid;
long cmd;
long arg1;
long arg2;
char text[MSG_BODY_TEXT_SIZE];
} msg_body_t, *msg_body_p;
typedef struct msg {
long dst_pid; // msg mtype is dest PID
msg_body_t body;
} msg_t, *msg_p;
int msg_qid;
void flush_msg_queue() {
msg_t msg;
do {
msgrcv(msg_qid, &msg, sizeof(msg_body_t), 0, IPC_NOWAIT);
} while (errno != ENOMSG);
}
void init_msg_queue() {
key_t msg_key = 0xdeadbeef;
int msg_flg = 0660 | IPC_CREAT;
msg_qid = msgget(msg_key, msg_flg);
if (msg_qid < 0) {
numad_log(LOG_CRIT, "msgget failed\n");
exit(EXIT_FAILURE);
}
flush_msg_queue();
}
void recv_msg(msg_p m) {
if (msgrcv(msg_qid, m, sizeof(msg_body_t), getpid(), 0) < 0) {
numad_log(LOG_CRIT, "msgrcv failed\n");
exit(EXIT_FAILURE);
}
// printf("Received: >>%s<< from process %d\n", m->body.text, m->body.src_pid);
}
void send_msg(long dst_pid, long cmd, long arg1, long arg2, char *s) {
msg_t msg;
msg.dst_pid = dst_pid;
msg.body.src_pid = getpid();
msg.body.cmd = cmd;
msg.body.arg1 = arg1;
msg.body.arg2 = arg2;
int s_len = strlen(s);
if (s_len >= MSG_BODY_TEXT_SIZE) {
numad_log(LOG_CRIT, "msgsnd text too big\n");
exit(EXIT_FAILURE);
}
strcpy(msg.body.text, s);
size_t m_len = sizeof(msg_body_t) - MSG_BODY_TEXT_SIZE + s_len + 1;
if (msgsnd(msg_qid, &msg, m_len, IPC_NOWAIT) < 0) {
numad_log(LOG_CRIT, "msgsnd failed\n");
exit(EXIT_FAILURE);
}
// printf("Sent: >>%s<< to process %d\n", msg.body.text, msg.dst_pid);
}
typedef struct id_list {
// Use CPU_SET(3) <sched.h> bitmasks,
// but bundle size and pointer together
// and genericize for both CPU and Node IDs
cpu_set_t *set_p;
size_t bytes;
} id_list_t, *id_list_p;
#define ID_LIST_SET_P(list_p) (list_p->set_p)
#define ID_LIST_BYTES(list_p) (list_p->bytes)
#define INIT_ID_LIST(list_p, num_elements) \
list_p = malloc(sizeof(id_list_t)); \
if (list_p == NULL) { numad_log(LOG_CRIT, "INIT_ID_LIST malloc failed\n"); exit(EXIT_FAILURE); } \
list_p->set_p = CPU_ALLOC(num_elements); \
if (list_p->set_p == NULL) { numad_log(LOG_CRIT, "CPU_ALLOC failed\n"); exit(EXIT_FAILURE); } \
list_p->bytes = CPU_ALLOC_SIZE(num_elements);
#define CLEAR_CPU_LIST(list_p) \
if (list_p == NULL) { \
INIT_ID_LIST(list_p, num_cpus); \
} \
CPU_ZERO_S(list_p->bytes, list_p->set_p)
#define CLEAR_NODE_LIST(list_p) \
if (list_p == NULL) { \
INIT_ID_LIST(list_p, num_nodes); \
} \
CPU_ZERO_S(list_p->bytes, list_p->set_p)
#define FREE_LIST(list_p) \
if (list_p != NULL) { \
if (list_p->set_p != NULL) { CPU_FREE(list_p->set_p); } \
free(list_p); \
list_p = NULL; \
}
#define COPY_LIST(orig_list_p, copy_list_p) \
memcpy(copy_list_p->set_p, orig_list_p->set_p, orig_list_p->bytes)
#define NUM_IDS_IN_LIST(list_p) CPU_COUNT_S(list_p->bytes, list_p->set_p)
#define ADD_ID_TO_LIST(k, list_p) CPU_SET_S(k, list_p->bytes, list_p->set_p)
#define CLR_ID_IN_LIST(k, list_p) CPU_CLR_S(k, list_p->bytes, list_p->set_p)
#define ID_IS_IN_LIST(k, list_p) CPU_ISSET_S(k, list_p->bytes, list_p->set_p)
#define EQUAL_LISTS(list_1_p, list_2_p) CPU_EQUAL_S(list_1_p->bytes, list_1_p->set_p, list_2_p->set_p)
#define AND_LISTS(and_list_p, list_1_p, list_2_p) CPU_AND_S(and_list_p->bytes, and_list_p->set_p, list_1_p->set_p, list_2_p->set_p)
#define OR_LISTS( or_list_p, list_1_p, list_2_p) CPU_OR_S( or_list_p->bytes, or_list_p->set_p, list_1_p->set_p, list_2_p->set_p)
#define XOR_LISTS(xor_list_p, list_1_p, list_2_p) CPU_XOR_S(xor_list_p->bytes, xor_list_p->set_p, list_1_p->set_p, list_2_p->set_p)
int negate_cpu_list(id_list_p list_p) {
if (list_p == NULL) {
numad_log(LOG_CRIT, "Cannot negate a NULL list\n");
exit(EXIT_FAILURE);
}
if (num_cpus < 1) {
numad_log(LOG_CRIT, "No CPUs to negate in list!\n");
exit(EXIT_FAILURE);
}
for (int ix = 0; (ix < num_cpus); ix++) {
if (ID_IS_IN_LIST(ix, list_p)) {
CLR_ID_IN_LIST(ix, list_p);
} else {
ADD_ID_TO_LIST(ix, list_p);
}
}
return NUM_IDS_IN_LIST(list_p);
}
int add_ids_to_list_from_str(id_list_p list_p, char *s) {
if (list_p == NULL) {
numad_log(LOG_CRIT, "Cannot add to NULL list\n");
exit(EXIT_FAILURE);
}
if ((s == NULL) || (strlen(s) == 0)) {
goto return_list;
}
int in_range = 0;
int next_id = 0;
for (;;) {
// skip over non-digits
while (!isdigit(*s)) {
if ((*s == '\n') || (*s == '\0')) {
goto return_list;
}
if (*s++ == '-') {
in_range = 1;
}
}
int id;
CONVERT_DIGITS_TO_NUM(s, id);
if (!in_range) {
next_id = id;
}
for (; (next_id <= id); next_id++) {
ADD_ID_TO_LIST(next_id, list_p);
}
in_range = 0;
}
return_list:
return NUM_IDS_IN_LIST(list_p);
}
int str_from_id_list(char *str_p, int str_size, id_list_p list_p) {
char *p = str_p;
if ((p == NULL) || (str_size < 3)) {
numad_log(LOG_CRIT, "Bad string for ID listing\n");
exit(EXIT_FAILURE);
}
int n;
if ((list_p == NULL) || ((n = NUM_IDS_IN_LIST(list_p)) == 0)) {
goto terminate_string;
}
int id_range_start = -1;
for (int id = 0; ; id++) {
int id_in_list = (ID_IS_IN_LIST(id, list_p) != 0);
if ((id_in_list) && (id_range_start < 0)) {
id_range_start = id; // beginning an ID range
} else if ((!id_in_list) && (id_range_start >= 0)) {
// convert the range that just ended...
p += snprintf(p, (str_p + str_size - p - 1), "%d", id_range_start);
if (id - id_range_start > 1) {
*p++ = '-';
p += snprintf(p, (str_p + str_size - p - 1), "%d", (id - 1));
}
*p++ = ',';
id_range_start = -1; // no longer in a range
if (n <= 0) { break; } // exit only after finishing a range
}
n -= id_in_list;
}
p -= 1; // eliminate trailing ','
terminate_string:
*p = '\0';
return (p - str_p);
}
typedef struct node_data {
uint64_t node_id;
uint64_t MBs_total;
uint64_t MBs_free;
uint64_t CPUs_total; // scaled * ONE_HUNDRED
uint64_t CPUs_free; // scaled * ONE_HUNDRED
uint64_t magnitude; // hack: MBs * CPUs
uint8_t *distance;
id_list_p cpu_list_p;
} node_data_t, *node_data_p;
node_data_p node = NULL;
int min_node_CPUs_free_ix = -1;
int min_node_MBs_free_ix = -1;
long min_node_CPUs_free = MAXINT;
long min_node_MBs_free = MAXINT;
long max_node_CPUs_free = 0;
long max_node_MBs_free = 0;
long avg_node_CPUs_free = 0;
long avg_node_MBs_free = 0;
double stddev_node_CPUs_free = 0.0;
double stddev_node_MBs_free = 0.0;
// RING_BUF_SIZE must be a power of two
#define RING_BUF_SIZE 8
#define PROCESS_FLAG_INTERLEAVED (1 << 0)
typedef struct process_data {
int pid;
unsigned int flags;
uint64_t data_time_stamp; // hundredths of seconds
uint64_t bind_time_stamp;
uint64_t num_threads;
uint64_t MBs_size;
uint64_t MBs_used;
uint64_t cpu_util;
uint64_t CPUs_used; // scaled * ONE_HUNDRED
uint64_t CPUs_used_ring_buf[RING_BUF_SIZE];
int ring_buf_ix;
char *comm;
id_list_p node_list_p;
uint64_t *process_MBs;
} process_data_t, *process_data_p;
// Hash table size must always be a power of two
#define MIN_PROCESS_HASH_TABLE_SIZE 16
int process_hash_table_size = 0;
int process_hash_collisions = 0;
process_data_p process_hash_table = NULL;
int process_hash_ix(int pid) {
unsigned ix = pid;
ix *= 717;
ix >>= 8;
ix &= (process_hash_table_size - 1);
return ix;
}
int process_hash_lookup(int pid) {
int ix = process_hash_ix(pid);
int starting_ix = ix;
while (process_hash_table[ix].pid) {
// Assumes table with some blank entries...
if (pid == process_hash_table[ix].pid) {
return ix; // found it
}
ix += 1;
ix &= (process_hash_table_size - 1);
if (ix == starting_ix) {
// Table full and pid not found.
// This "should never happen"...
break;
}
}
return -1;
}
int process_hash_insert(int pid) {
// This reserves the hash table slot, but initializes only the pid field
int ix = process_hash_ix(pid);
int starting_ix = ix;
while (process_hash_table[ix].pid) {
if (pid == process_hash_table[ix].pid) {
return ix; // found it
}
process_hash_collisions += 1;
ix += 1;
ix &= (process_hash_table_size - 1);
if (ix == starting_ix) {
// This "should never happen"...
numad_log(LOG_ERR, "Process hash table is full\n");
return -1;
}
}
process_hash_table[ix].pid = pid;
return ix;
}
int process_hash_update(process_data_p newp) {
// This updates hash table stats for processes we are monitoring. Only the
// scalar resource consumption stats need to be updated here.
int new_hash_table_entry = 1;
int ix = process_hash_insert(newp->pid);
if (ix >= 0) {
process_data_p p = &process_hash_table[ix];
if (p->data_time_stamp) {
new_hash_table_entry = 0;
p->ring_buf_ix += 1;
p->ring_buf_ix &= (RING_BUF_SIZE - 1);
uint64_t cpu_util_diff = newp->cpu_util - p->cpu_util;
uint64_t time_diff = newp->data_time_stamp - p->data_time_stamp;
p->CPUs_used_ring_buf[p->ring_buf_ix] = 100 * (cpu_util_diff) / time_diff;
// Use largest CPU utilization currently in ring buffer
uint64_t max_CPUs_used = p->CPUs_used_ring_buf[0];
for (int ix = 1; (ix < RING_BUF_SIZE); ix++) {
if (max_CPUs_used < p->CPUs_used_ring_buf[ix]) {
max_CPUs_used = p->CPUs_used_ring_buf[ix];
}
}
p->CPUs_used = max_CPUs_used;
}
if ((!p->comm) || (strcmp(p->comm, newp->comm))) {
if (p->comm) {
free(p->comm);
}
p->comm = strdup(newp->comm);
}
p->MBs_size = newp->MBs_size;
p->MBs_used = newp->MBs_used;
p->cpu_util = newp->cpu_util;
p->num_threads = newp->num_threads;
p->data_time_stamp = newp->data_time_stamp;
}
return new_hash_table_entry;
}
void process_hash_clear_all_bind_time_stamps() {
for (int ix = 0; (ix < process_hash_table_size); ix++) {
process_hash_table[ix].bind_time_stamp = 0;
}
}
int process_hash_rehash(int old_ix) {
// Given the index of a table entry that would otherwise be orphaned by
// process_hash_remove(), reinsert into table using PID from existing record.
process_data_p op = &process_hash_table[old_ix];
int new_ix = process_hash_insert(op->pid);
if (new_ix >= 0) {
// Copy old slot to new slot, and zero old slot
process_data_p np = &process_hash_table[new_ix];
memcpy(np, op, sizeof(process_data_t));
memset(op, 0, sizeof(process_data_t));
}
return new_ix;
}
int process_hash_remove(int pid) {
int ix = process_hash_lookup(pid);
if (ix >= 0) {
// remove the target
process_data_p dp = &process_hash_table[ix];
if (dp->comm) { free(dp->comm); }
if (dp->process_MBs) { free(dp->process_MBs); }
FREE_LIST(dp->node_list_p);
memset(dp, 0, sizeof(process_data_t));
// bubble up the collision chain and rehash if neeeded
for (;;) {
ix += 1;
ix &= (process_hash_table_size - 1);
if ((pid = process_hash_table[ix].pid) <= 0) {
break;
}
if (process_hash_lookup(pid) < 0) {
if (process_hash_rehash(ix) < 0) {
numad_log(LOG_ERR, "rehash fail\n");
}
}
}
}
return ix;
}
void process_hash_table_expand() {
// Save old table size and address
int old_size = process_hash_table_size;
process_data_p old_table = process_hash_table;
// Double size of table and allocate new space
if (old_size > 0) {
process_hash_table_size *= 2;
} else {
process_hash_table_size = MIN_PROCESS_HASH_TABLE_SIZE;
}
numad_log(LOG_DEBUG, "Expanding hash table size: %d\n", process_hash_table_size);
process_hash_table = malloc(process_hash_table_size * sizeof(process_data_t));
if (process_hash_table == NULL) {
numad_log(LOG_CRIT, "hash table malloc failed\n");
exit(EXIT_FAILURE);
}
// Clear the new table, and copy valid entries from old table
memset(process_hash_table, 0, process_hash_table_size * sizeof(process_data_t));
for (int ix = 0; (ix < old_size); ix++) {
process_data_p p = &old_table[ix];
if (p->pid) {
int new_table_ix = process_hash_insert(p->pid);
memcpy(&process_hash_table[new_table_ix], p, sizeof(process_data_t));
}
}
if (old_table != NULL) {
free(old_table);
}
}
void process_hash_table_dump() {
for (int ix = 0; (ix < process_hash_table_size); ix++) {
process_data_p p = &process_hash_table[ix];
if (p->pid) {
numad_log(LOG_DEBUG,
"ix: %d PID: %d %s Thds: %d CPU %ld MBs: %ld/%ld Data TS: %ld Bind TS: %ld\n",
ix, p->pid, ((p->comm != NULL) ? p->comm : "(Null)"), p->num_threads,
p->CPUs_used, p->MBs_used, p->MBs_size, p->data_time_stamp, p->bind_time_stamp);
// FIXME: make this dump every field, but this is not even currently used
}
}
}
void process_hash_table_cleanup(uint64_t update_time) {
int num_hash_entries_used = 0;
for (int ix = 0; (ix < process_hash_table_size); ix++) {
process_data_p p = &process_hash_table[ix];
if (p->pid) {
num_hash_entries_used += 1;
if (p->data_time_stamp < update_time) {
// Mark as old, and zero CPU utilization
p->data_time_stamp = 0;
p->CPUs_used = 0;
// Check for dead pids and remove them...
if ((kill(p->pid, 0) == -1) && (errno == ESRCH)) {
// Seems dead. Forget this pid
process_hash_remove(p->pid);
num_hash_entries_used -= 1;
}
}
}
}
// Keep hash table approximately half empty
if ((num_hash_entries_used * 7) / 4 > process_hash_table_size) {
process_hash_table_expand();
}
}
typedef struct pid_list {
long pid;
struct pid_list* next;
} pid_list_t, *pid_list_p;
pid_list_p include_pid_list = NULL;
pid_list_p exclude_pid_list = NULL;
pid_list_p insert_pid_into_pid_list(pid_list_p list_ptr, long pid) {
if (process_hash_table != NULL) {
int hash_ix = process_hash_lookup(pid);
if ((hash_ix >= 0) && (list_ptr == include_pid_list)) {
// Clear interleaved flag, in case user wants it to be re-evaluated
process_hash_table[hash_ix].flags &= ~PROCESS_FLAG_INTERLEAVED;
}
}
// Check for duplicate pid first
pid_list_p pid_ptr = list_ptr;
while (pid_ptr != NULL) {
if (pid_ptr->pid == pid) {
// pid already in list
return list_ptr;
}
pid_ptr = pid_ptr->next;
}
// pid not yet in list -- insert new node
pid_ptr = malloc(sizeof(pid_list_t));
if (pid_ptr == NULL) {
numad_log(LOG_CRIT, "pid_list malloc failed\n");
exit(EXIT_FAILURE);
}
pid_ptr->pid = pid;
pid_ptr->next = list_ptr;
list_ptr = pid_ptr;
return list_ptr;
}
pid_list_p remove_pid_from_pid_list(pid_list_p list_ptr, long pid) {
pid_list_p last_pid_ptr = NULL;
pid_list_p pid_ptr = list_ptr;
while (pid_ptr != NULL) {
if (pid_ptr->pid == pid) {
if (pid_ptr == list_ptr) {
list_ptr = list_ptr->next;
free(pid_ptr);
pid_ptr = list_ptr;
continue;
} else {
last_pid_ptr->next = pid_ptr->next;
free(pid_ptr);
pid_ptr = last_pid_ptr;
}
}
last_pid_ptr = pid_ptr;
pid_ptr = pid_ptr->next;
}
return list_ptr;
}
void shut_down_numad() {
numad_log(LOG_NOTICE, "Shutting down numad\n");
flush_msg_queue();
unlink(VAR_RUN_FILE);
close_log_file();
exit(EXIT_SUCCESS);
}
void print_version_and_exit(char *prog_name) {
fprintf(stdout, "%s version: %s: compiled %s\n", prog_name, VERSION_STRING, __DATE__);
exit(EXIT_SUCCESS);
}
void print_usage_and_exit(char *prog_name) {
fprintf(stderr, "Usage: %s <options> ...\n", prog_name);
fprintf(stderr, "-C 1 to count inactive file cache as available memory (default 1)\n");
fprintf(stderr, "-C 0 to count inactive file cache memory as unavailable (default 1)\n");
fprintf(stderr, "-d for debug logging (same effect as '-l 7')\n");
fprintf(stderr, "-h to print this usage info\n");
fprintf(stderr, "-H <N> to set THP scan_sleep_ms (default %d)\n", DEFAULT_THP_SCAN_SLEEP_MS);
fprintf(stderr, "-i [<MIN>:]<MAX> to specify interval seconds\n");
fprintf(stderr, "-K 1 to keep interleaved memory spread across nodes (default 0)\n");
fprintf(stderr, "-K 0 to merge interleaved memory to local NUMA nodes (default 0)\n");
fprintf(stderr, "-l <N> to specify logging level (usually 5, 6, or 7 -- default 5)\n");
fprintf(stderr, "-m <N> to specify memory locality target percent (default %d)\n", DEFAULT_MEMLOCALITY_PERCENT);
fprintf(stderr, "-p <PID> to add PID to inclusion pid list\n");
fprintf(stderr, "-r <PID> to remove PID from explicit pid lists\n");
fprintf(stderr, "-R <CPU_LIST> to reserve some CPUs for non-numad use\n");
fprintf(stderr, "-S 1 to scan all processes (default 1)\n");
fprintf(stderr, "-S 0 to scan only explicit PID list processes (default 1)\n");
fprintf(stderr, "-t <N> to specify thread / logical CPU valuation percent (default %d)\n", DEFAULT_HTT_PERCENT);
fprintf(stderr, "-u <N> to specify utilization target percent (default %d)\n", DEFAULT_UTILIZATION_PERCENT);
fprintf(stderr, "-v for verbose (same effect as '-l 6')\n");
fprintf(stderr, "-V to show version info\n");
fprintf(stderr, "-w <CPUs>[:<MBs>] for NUMA node suggestions\n");
fprintf(stderr, "-x <PID> to add PID to exclusion pid list\n");
exit(EXIT_FAILURE);
}
void set_thp_scan_sleep_ms(int new_ms) {
if (new_ms < 1) {
// 0 means do not change the system default
return;
}
char *thp_scan_fname = "/sys/kernel/mm/transparent_hugepage/khugepaged/scan_sleep_millisecs";
int fd = open(thp_scan_fname, O_RDWR, 0);
if (fd >= 0) {
char buf[BUF_SIZE];
int bytes = read(fd, buf, BUF_SIZE);
if (bytes > 0) {
buf[bytes] = '\0';
int cur_ms;
char *p = buf;
CONVERT_DIGITS_TO_NUM(p, cur_ms);
if (cur_ms != new_ms) {
lseek(fd, 0, SEEK_SET);
numad_log(LOG_NOTICE, "Changing THP scan time in %s from %d to %d ms.\n", thp_scan_fname, cur_ms, new_ms);
sprintf(buf, "%d\n", new_ms);
write(fd, buf, strlen(buf));
}
}
close(fd);
}
}
void check_prereqs(char *prog_name) {
// Adjust kernel tunable to scan for THP more frequently...
set_thp_scan_sleep_ms(thp_scan_sleep_ms);
}
int get_daemon_pid() {
int fd = open(VAR_RUN_FILE, O_RDONLY, 0);
if (fd < 0) {
return 0;
}
char buf[BUF_SIZE];
int bytes = read(fd, buf, BUF_SIZE);
close(fd);
if (bytes <= 0) {
return 0;
}
int pid;
char *p = buf;
CONVERT_DIGITS_TO_NUM(p, pid);
// Check run file pid still active
char fname[FNAME_SIZE];
snprintf(fname, FNAME_SIZE, "/proc/%d", pid);
if (access(fname, F_OK) < 0) {
if (errno == ENOENT) {
numad_log(LOG_NOTICE, "Removing out-of-date numad run file because %s doesn't exist\n", fname);
unlink(VAR_RUN_FILE);
}
return 0;
}
// Daemon must be running already.
return pid;
}
int register_numad_pid() {
int pid;
char buf[BUF_SIZE];
int fd;
create_run_file:
fd = open(VAR_RUN_FILE, O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
if (fd >= 0) {
pid = getpid();
sprintf(buf, "%d\n", pid);
write(fd, buf, strlen(buf));
close(fd);
numad_log(LOG_NOTICE, "Registering numad version %s PID %d\n", VERSION_STRING, pid);
return pid;
}
if (errno == EEXIST) {
fd = open(VAR_RUN_FILE, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
if (fd < 0) {
goto fail_numad_run_file;
}
int bytes = read(fd, buf, BUF_SIZE);
close(fd);
if (bytes > 0) {
char *p = buf;
CONVERT_DIGITS_TO_NUM(p, pid);
// Check pid in run file still active
char fname[FNAME_SIZE];
snprintf(fname, FNAME_SIZE, "/proc/%d", pid);
if (access(fname, F_OK) < 0) {
if (errno == ENOENT) {
// Assume run file is out-of-date...
numad_log(LOG_NOTICE, "Removing out-of-date numad run file because %s doesn't exist\n", fname);
unlink(VAR_RUN_FILE);
goto create_run_file;
}
}
// Daemon must be running already.
return pid;
}
}
fail_numad_run_file:
numad_log(LOG_CRIT, "Cannot open numad.pid file\n");
exit(EXIT_FAILURE);
}
int count_set_bits_in_hex_list_file(char *fname) {
int sum = 0;
int fd = open(fname, O_RDONLY, 0);
if (fd >= 0) {
char buf[BUF_SIZE];
int bytes = read(fd, buf, BUF_SIZE);
close(fd);
for (int ix = 0; (ix < bytes); ix++) {
char c = tolower(buf[ix]);
switch (c) {
case '0' : sum += 0; break;
case '1' : sum += 1; break;
case '2' : sum += 1; break;
case '3' : sum += 2; break;
case '4' : sum += 1; break;
case '5' : sum += 2; break;
case '6' : sum += 2; break;
case '7' : sum += 3; break;
case '8' : sum += 1; break;
case '9' : sum += 2; break;
case 'a' : sum += 2; break;
case 'b' : sum += 3; break;
case 'c' : sum += 2; break;
case 'd' : sum += 3; break;
case 'e' : sum += 3; break;
case 'f' : sum += 4; break;
case ' ' : sum += 0; break;
case ',' : sum += 0; break;
case '\n' : sum += 0; break;
default : numad_log(LOG_CRIT, "Unexpected character in list\n"); exit(EXIT_FAILURE);
}
}
}
return sum;
}
int get_num_cpus() {
int n1 = sysconf(_SC_NPROCESSORS_CONF);
int n2 = sysconf(_SC_NPROCESSORS_ONLN);
if (n1 < n2) {
n1 = n2;
}
if (n1 < 0) {
numad_log(LOG_CRIT, "Cannot count number of processors\n");
exit(EXIT_FAILURE);
}
return n1;
}
int get_num_kvm_vcpu_threads(int pid) {
// Try to return the number of vCPU threads for this VM guest,
// excluding the IO threads. All failures return MAXINT.
// FIXME: someday figure out some better way to do this...
char fname[FNAME_SIZE];
snprintf(fname, FNAME_SIZE, "/proc/%d/cmdline", pid);
int fd = open(fname, O_RDONLY, 0);
if (fd >= 0) {
char buf[BUF_SIZE];
int bytes = read(fd, buf, BUF_SIZE);
close(fd);
if (bytes > 0) {
char *p = memmem(buf, bytes, "smp", 3);
if (p != NULL) {
while (!isdigit(*p) && (p - buf < bytes - 2)) {
p++;
}
if (isdigit(*p)) {
int vcpu_threads;
CONVERT_DIGITS_TO_NUM(p, vcpu_threads);
if ((vcpu_threads > 0) && (vcpu_threads <= num_cpus)) {
return vcpu_threads;
}
}
}
}
}
return MAXINT;
}
uint64_t get_huge_page_size_in_bytes() {
uint64_t huge_page_size = 0;;
FILE *fs = fopen("/proc/meminfo", "r");
if (!fs) {
numad_log(LOG_CRIT, "Can't open /proc/meminfo\n");
exit(EXIT_FAILURE);
}
char buf[BUF_SIZE];
while (fgets(buf, BUF_SIZE, fs)) {
if (!strncmp("Hugepagesize", buf, 12)) {
char *p = &buf[12];
while ((!isdigit(*p)) && (p < buf + BUF_SIZE)) {
p++;
}
huge_page_size = atol(p);
break;
}
}
fclose(fs);
return huge_page_size * KILOBYTE;
}
uint64_t get_time_stamp() {
// Return time stamp in hundredths of a second
struct timespec ts;
if (clock_gettime(CLOCK_MONOTONIC, &ts) < 0) {
numad_log(LOG_CRIT, "Cannot get clock_gettime()\n");
exit(EXIT_FAILURE);
}
return (ts.tv_sec * ONE_HUNDRED) +
(ts.tv_nsec / (1000000000 / ONE_HUNDRED));
}
static int name_starts_with_digit(const struct dirent *dptr) {
return (isdigit(dptr->d_name[0]));
}
#define BITS_IN_LONG (CHAR_BIT * sizeof(unsigned long))
#define SET_BIT(i,a) (a)[(i) / BITS_IN_LONG] |= (1u << ((i) % BITS_IN_LONG))
#define TEST_BIT(i,a) (((a)[(i) / BITS_IN_LONG] & (1u << ((i) % BITS_IN_LONG))) != 0)
#define CLEAR_BIT(i,a) (a)[(i) / BITS_IN_LONG] &= ~(1u << ((i) % BITS_IN_LONG))
int bind_process_and_migrate_memory(process_data_p p) {
uint64_t t0 = get_time_stamp();
// Parameter p is a pointer to an element in the hash table
if ((!p) || (p->pid < 1)) {
numad_log(LOG_CRIT, "Bad PID to bind\n");
exit(EXIT_FAILURE);
}
if (!p->node_list_p) {
numad_log(LOG_CRIT, "Cannot bind to unspecified node(s)\n");
exit(EXIT_FAILURE);
}
// Generate CPU list derived from target node list.
static id_list_p cpu_bind_list_p;
CLEAR_CPU_LIST(cpu_bind_list_p);
int nodes = NUM_IDS_IN_LIST(p->node_list_p);
int node_id = 0;
while (nodes) {
if (ID_IS_IN_LIST(node_id, p->node_list_p)) {
OR_LISTS(cpu_bind_list_p, cpu_bind_list_p, node[node_id].cpu_list_p);
nodes -= 1;
}