forked from TelegramMessenger/MTProxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjobs.c
1706 lines (1491 loc) · 49.1 KB
/
jobs.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
/*
This file is part of Mtproto-proxy Library.
Mtproto-proxy Library 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, either version 2 of the License, or
(at your option) any later version.
Mtproto-proxy Library 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 have received a copy of the GNU Lesser General Public License
along with Mtproto-proxy Library. If not, see <http://www.gnu.org/licenses/>.
Copyright 2014-2015 Telegram Messenger Inc
2014-2015 Nikolai Durov
2014 Andrey Lopatin
*/
#define _FILE_OFFSET_BITS 64
#define _XOPEN_SOURCE 500
#define _GNU_SOURCE 1
#include <assert.h>
#include <errno.h>
#include <pthread.h>
#include <signal.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <malloc.h>
#include <sys/syscall.h>
#include <math.h>
#include <linux/futex.h>
#include "common/proc-stat.h"
#include "crc32.h"
#include "net/net-events.h"
//#include "net/net-buffers.h"
#include "server-functions.h"
#include "kprintf.h"
#include "precise-time.h"
#include "mp-queue.h"
#include "net/net-connections.h"
#include "jobs/jobs.h"
#include "common/common-stats.h"
//#include "auto/engine/engine.h"
#define JOB_SUBCLASS_OFFSET 3
struct job_thread JobThreads[MAX_JOB_THREADS] __attribute__((aligned(128)));
struct job_thread_stat {
unsigned long tot_sys;
unsigned long tot_user;
unsigned long recent_sys;
unsigned long recent_user;
};
struct job_thread_stat JobThreadsStats[MAX_JOB_THREADS] __attribute__((aligned(128)));
#define MODULE jobs
MODULE_STAT_TYPE {
double tot_idle_time, a_idle_time, a_idle_quotient;
long long jobs_allocated_memory;
int jobs_ran;
int job_timers_allocated;
double locked_since;
long long timer_ops;
long long timer_ops_scheduler;
};
MODULE_INIT
MODULE_STAT_FUNCTION
int uptime = time (0) - start_time;
double tm = get_utime_monotonic ();
double tot_recent_idle[16];
double tot_recent_q[16];
double tot_idle[16];
int tot_threads[16];
memset (tot_recent_idle, 0, sizeof (tot_recent_idle));
memset (tot_recent_q, 0, sizeof (tot_recent_q));
memset (tot_idle, 0, sizeof (tot_idle));
memset (tot_threads, 0, sizeof (tot_threads));
tot_recent_idle[JC_MAIN] = a_idle_time;
tot_recent_q[JC_MAIN] = a_idle_quotient;
tot_idle[JC_MAIN] = tot_idle_time;
int i,j;
for (i = 0; i < max_job_thread_id + 1; i++) {
if (MODULE_STAT_ARR[i]) {
assert (JobThreads[i].id == i);
int class = JobThreads[i].thread_class & JC_MASK;
tot_recent_idle[class] += MODULE_STAT_ARR[i]->a_idle_time;
tot_recent_q[class] += MODULE_STAT_ARR[i]->a_idle_quotient;
tot_idle[class] += MODULE_STAT_ARR[i]->tot_idle_time;
if (MODULE_STAT_ARR[i]->locked_since) {
double lt = MODULE_STAT_ARR[i]->locked_since;
tot_recent_idle[class] += (tm - lt);
tot_recent_q[class] += (tm - lt);
tot_idle[class] += (tm - lt);
}
tot_threads[class] ++;
}
}
sb_printf (sb, "thread_average_idle_percent\t");
for (i = 0; i < 16; i++) {
if (i != 0) {
sb_printf (sb, " ");
if (!(i & 3)) {
sb_printf (sb, " ");
}
}
sb_printf (sb, "%.3f", safe_div (tot_idle[i], uptime * tot_threads[i]) * 100);
}
sb_printf (sb, "\n");
sb_printf (sb, "thread_recent_idle_percent\t");
for (i = 0; i < 16; i++) {
if (i != 0) {
sb_printf (sb, " ");
if (!(i & 3)) {
sb_printf (sb, " ");
}
}
sb_printf (sb, "%.3f", safe_div (tot_recent_idle[i], tot_recent_q[i]) * 100);
}
sb_printf (sb, "\n");
sb_printf (sb, "tot_threads\t");
for (i = 0; i < 16; i++) {
if (i != 0) {
sb_printf (sb, " ");
if (!(i & 3)) {
sb_printf (sb, " ");
}
}
sb_printf (sb, "%d", tot_threads[i]);
}
sb_printf (sb, "\n");
double jb_cpu_load_u[16];
double jb_cpu_load_s[16];
double jb_cpu_load_t[16];
double jb_cpu_load_ru[16];
double jb_cpu_load_rs[16];
double jb_cpu_load_rt[16];
memset (jb_cpu_load_u, 0, sizeof (jb_cpu_load_u));
memset (jb_cpu_load_s, 0, sizeof (jb_cpu_load_u));
memset (jb_cpu_load_t, 0, sizeof (jb_cpu_load_u));
memset (jb_cpu_load_ru, 0, sizeof (jb_cpu_load_u));
memset (jb_cpu_load_rs, 0, sizeof (jb_cpu_load_u));
memset (jb_cpu_load_rt, 0, sizeof (jb_cpu_load_u));
double tot_cpu_load_u = 0;
double tot_cpu_load_s = 0;
double tot_cpu_load_t = 0;
double tot_cpu_load_ru = 0;
double tot_cpu_load_rs = 0;
double tot_cpu_load_rt = 0;
double max_cpu_load_u = 0;
double max_cpu_load_s = 0;
double max_cpu_load_t = 0;
double max_cpu_load_ru = 0;
double max_cpu_load_rs = 0;
double max_cpu_load_rt = 0;
for (i = 0; i < max_job_thread_id + 1; i++) {
if (MODULE_STAT_ARR[i]) {
assert (JobThreads[i].id == i);
int class = JobThreads[i].thread_class & JC_MASK;
jb_cpu_load_u[class] += JobThreadsStats[i].tot_user;
jb_cpu_load_s[class] += JobThreadsStats[i].tot_sys;
jb_cpu_load_t[class] += JobThreadsStats[i].tot_user + JobThreadsStats[i].tot_sys;
jb_cpu_load_ru[class] += JobThreadsStats[i].recent_user;
jb_cpu_load_rs[class] += JobThreadsStats[i].recent_sys;
jb_cpu_load_rt[class] += JobThreadsStats[i].recent_user + JobThreadsStats[i].recent_sys;
}
}
for (i = 0; i < 16; i++) {
tot_cpu_load_u += jb_cpu_load_u[i];
tot_cpu_load_s += jb_cpu_load_s[i];
tot_cpu_load_t += jb_cpu_load_t[i];
tot_cpu_load_ru += jb_cpu_load_ru[i];
tot_cpu_load_rs += jb_cpu_load_rs[i];
tot_cpu_load_rt += jb_cpu_load_rt[i];
#define max(a,b) (a) > (b) ? (a) : (b)
max_cpu_load_u = max (max_cpu_load_u, jb_cpu_load_u[i]);
max_cpu_load_s = max (max_cpu_load_s, jb_cpu_load_s[i]);
max_cpu_load_t = max (max_cpu_load_t, jb_cpu_load_t[i]);
max_cpu_load_ru = max (max_cpu_load_ru, jb_cpu_load_ru[i]);
max_cpu_load_rs = max (max_cpu_load_rs, jb_cpu_load_rs[i]);
max_cpu_load_rt = max (max_cpu_load_rt, jb_cpu_load_rt[i]);
#undef max
}
const double m_clk_to_hs = 100.0 / sysconf (_SC_CLK_TCK); /* hundredth of a second */
const double m_clk_to_ts = 0.1 * m_clk_to_hs; /* tenth of a second */
for (j = 0; j < 6; j++) {
double *b = NULL;
double d = 0;
switch (j) {
case 0:
sb_printf (sb, "thread_load_average_user\t");
b = jb_cpu_load_u;
d = uptime;
break;
case 1:
sb_printf (sb, "thread_load_average_sys\t");
b = jb_cpu_load_s;
d = uptime;
break;
case 2:
sb_printf (sb, "thread_load_average\t");
b = jb_cpu_load_t;
d = uptime;
break;
case 3:
sb_printf (sb, "thread_load_recent_user\t");
b = jb_cpu_load_ru;
d = 10;
break;
case 4:
sb_printf (sb, "thread_load_recent_sys\t");
b = jb_cpu_load_rs;
d = 10;
break;
case 5:
sb_printf (sb, "thread_load_recent\t");
b = jb_cpu_load_rt;
d = 10;
break;
default:
assert (0);
}
for (i = 0; i < 16; i++) {
if (i != 0) {
sb_printf (sb, " ");
if (!(i & 3)) {
sb_printf (sb, " ");
}
}
sb_printf (sb, "%.3f", safe_div (m_clk_to_hs * b[i], d * tot_threads[i]));
}
sb_printf (sb, "\n");
}
sb_printf (sb,
"load_average_user\t%.3f\n"
"load_average_sys\t%.3f\n"
"load_average_total\t%.3f\n"
"load_recent_user\t%.3f\n"
"load_recent_sys\t%.3f\n"
"load_recent_total\t%.3f\n"
"max_average_user\t%.3f\n"
"max_average_sys\t%.3f\n"
"max_average_total\t%.3f\n"
"max_recent_user\t%.3f\n"
"max_recent_sys\t%.3f\n"
"max_recent_total\t%.3f\n",
safe_div (m_clk_to_hs * tot_cpu_load_u, uptime),
safe_div (m_clk_to_hs * tot_cpu_load_s, uptime),
safe_div (m_clk_to_hs * tot_cpu_load_t, uptime),
m_clk_to_ts * tot_cpu_load_ru,
m_clk_to_ts * tot_cpu_load_rs,
m_clk_to_ts * tot_cpu_load_rt,
safe_div (m_clk_to_hs * max_cpu_load_u, uptime),
safe_div (m_clk_to_hs * max_cpu_load_s, uptime),
safe_div (m_clk_to_hs * max_cpu_load_t, uptime),
m_clk_to_ts * max_cpu_load_ru,
m_clk_to_ts * max_cpu_load_rs,
m_clk_to_ts * max_cpu_load_rt
);
SB_SUM_ONE_I (job_timers_allocated);
int jb_running[16], jb_active = 0;
long long jb_created = 0;
memset (jb_running, 0, sizeof (jb_running));
for (i = 1; i <= max_job_thread_id; i++) {
struct job_thread *JT = &JobThreads[i];
if (JT->status) {
jb_active += JT->jobs_active;
jb_created += JT->jobs_created;
for (j = 0; j <= JC_MAX; j++) {
jb_running[j] += JT->jobs_running[j];
}
}
}
sb_printf (sb,
"jobs_created\t%lld\n"
"jobs_active\t%d\n",
jb_created,
jb_active
);
sb_printf (sb, "jobs_running\t");
for (i = 0; i < 16; i++) {
if (i != 0) {
sb_printf (sb, " ");
if (!(i & 3)) {
sb_printf (sb, " ");
}
}
sb_printf (sb, "%d", jb_running[i]);
}
sb_printf (sb, "\n");
SB_SUM_ONE_LL (jobs_allocated_memory);
SB_SUM_ONE_LL (timer_ops);
SB_SUM_ONE_LL (timer_ops_scheduler);
MODULE_STAT_FUNCTION_END
long long jobs_get_allocated_memoty (void) {
return SB_SUM_LL (jobs_allocated_memory);
}
void update_thread_stat (int pid, int tid, int id) {
struct proc_stats s;
if (!tid) { tid = pid; }
read_proc_stats (pid, tid, &s);
struct job_thread_stat *S = &JobThreadsStats[id];
S->recent_sys = (s.stime - S->tot_sys);
S->recent_user = (s.utime - S->tot_user);
S->tot_sys = s.stime;
S->tot_user = s.utime;
}
void update_all_thread_stats (void) {
int i;
pid_t pid = getpid ();
for (i = 1; i <= max_job_thread_id; i++) {
update_thread_stat (pid, JobThreads[i].thread_system_id, i);
}
}
void wakeup_main_thread (void) __attribute__ ((weak));
void wakeup_main_thread (void) {}
#define JOB_THREAD_STACK_SIZE (4 << 20)
#define JTS_CREATED 1
#define JTS_RUNNING 2
#define JTS_PERFORMING 4
struct job_class JobClasses[JC_MAX + 1];
int max_job_thread_id;
int cur_job_threads;
int main_pthread_id_initialized;
pthread_t main_pthread_id;
struct job_thread *main_job_thread;
__thread struct job_thread *this_job_thread;
__thread job_t this_job;
long int lrand48_j (void) {
if (this_job_thread) {
long int t;
lrand48_r (&this_job_thread->rand_data, &t);
return t;
} else {
return lrand48 ();
}
}
long int mrand48_j (void) {
if (this_job_thread) {
long int t;
mrand48_r (&this_job_thread->rand_data, &t);
return t;
} else {
return mrand48 ();
}
}
double drand48_j (void) {
if (this_job_thread) {
double t;
drand48_r (&this_job_thread->rand_data, &t);
return t;
} else {
return drand48 ();
}
}
struct mp_queue MainJobQueue __attribute__((aligned(128)));
static struct thread_callback *jobs_cb_list;
void init_main_pthread_id (void) {
pthread_t self = pthread_self ();
if (main_pthread_id_initialized) {
assert (pthread_equal (main_pthread_id, self));
} else {
main_pthread_id = self;
main_pthread_id_initialized = 1;
}
}
void check_main_thread (void) {
pthread_t self = pthread_self ();
assert (main_pthread_id_initialized && pthread_equal (main_pthread_id, self));
}
static void set_job_interrupt_signal_handler (void);
void *job_thread (void *arg);
void *job_thread_sub (void *arg);
int create_job_thread_ex (int thread_class, void *(*thread_work)(void *)) {
assert (!(thread_class & ~JC_MASK));
assert (thread_class);
assert ((thread_class != JC_MAIN) ^ !cur_job_threads);
if (cur_job_threads >= MAX_JOB_THREADS) {
return -1;
}
check_main_thread ();
struct job_class *JC = &JobClasses[thread_class];
if (thread_class != JC_MAIN && JC->job_queue == &MainJobQueue) {
assert (main_job_thread);
JC->job_queue = alloc_mp_queue_w ();
main_job_thread->job_class_mask &= ~(1 << thread_class);
/*if (max_job_class_threads[thread_class] == 1) {
run_pending_main_jobs ();
}*/
}
assert (JC->job_queue);;
int i;
struct job_thread *JT = 0;
for (i = 1; i < MAX_JOB_THREADS; i++) {
if (!JobThreads[i].status && !JobThreads[i].pthread_id) {
JT = &JobThreads[i];
break;
}
}
if (!JT) {
return -1;
}
memset (JT, 0, sizeof (struct job_thread));
JT->status = JTS_CREATED;
JT->thread_class = thread_class;
JT->job_class_mask = 1 | (thread_class == JC_MAIN ? 0xffff : (1 << thread_class));
JT->job_queue = JC->job_queue;
JT->job_class = JC;
JT->id = i;
assert (JT->job_queue);
srand48_r (rdtsc () ^ lrand48 (), &JT->rand_data);
if (thread_class != JC_MAIN) {
pthread_attr_t attr;
pthread_attr_init (&attr);
pthread_attr_setstacksize (&attr, JOB_THREAD_STACK_SIZE);
int r = pthread_create (&JT->pthread_id, &attr, thread_work, (void *) JT);
pthread_attr_destroy (&attr);
if (r) {
vkprintf (0, "create_job_thread: pthread_create() failed: %s\n", strerror (r));
JT->status = 0;
return -1;
}
} else {
assert (!main_job_thread);
get_this_thread_id ();
JT->pthread_id = main_pthread_id;
this_job_thread = main_job_thread = JT;
set_job_interrupt_signal_handler ();
assert (JT->id == 1);
}
if (i > max_job_thread_id) {
max_job_thread_id = i;
}
cur_job_threads++;
JC->cur_threads ++;
return i;
}
int create_job_thread (int thread_class) {
struct job_class *JC = &JobClasses[thread_class];
return create_job_thread_ex (thread_class, JC->subclasses ? job_thread_sub : job_thread);
}
int create_job_class_threads (int job_class) {
assert (job_class != JC_MAIN);
int created = 0;
assert (job_class >= 1 && job_class <= JC_MAX);
struct job_class *JC = &JobClasses[job_class];
assert (JC->min_threads <= JC->max_threads);
check_main_thread ();
while (JC->cur_threads < JC->min_threads && cur_job_threads < MAX_JOB_THREADS) {
assert (create_job_thread (job_class) >= 0);
created++;
}
return created;
}
int init_async_jobs (void) {
init_main_pthread_id ();
if (!MainJobQueue.mq_magic) {
init_mp_queue_w (&MainJobQueue);
int i;
for (i = 0; i < JC_MAX + 1; i++) {
JobClasses[i].job_queue = &MainJobQueue;
}
}
if (!cur_job_threads) {
assert (create_job_thread (JC_MAIN) >= 0);
}
/*
int i;
for (i = 1; i < 16; i++) if (i != JC_MAIN) {
create_job_class_threads (i);
}*/
return cur_job_threads;
}
int create_new_job_class (int job_class, int min_threads, int max_threads) {
return create_job_class (job_class, min_threads, max_threads, 1);
}
int create_new_job_class_sub (int job_class, int min_threads, int max_threads, int subclass_cnt) {
return create_job_class_sub(job_class, min_threads, max_threads, 1, subclass_cnt);
}
int create_job_class (int job_class, int min_threads, int max_threads, int excl) {
assert (job_class >= 1 && job_class <= JC_MAX);
assert (min_threads >= 0 && max_threads >= min_threads);
struct job_class *JC = &JobClasses[job_class];
assert (!excl || !JC->min_threads);
if (min_threads < JC->min_threads || !JC->min_threads) {
JC->min_threads = min_threads;
}
if (max_threads > JC->max_threads) {
JC->max_threads = max_threads;
}
assert (JC->min_threads <= JC->max_threads);
if (MainJobQueue.mq_magic) {
return create_job_class_threads (job_class);
} else {
return 0;
}
}
int create_job_class_sub (int job_class, int min_threads, int max_threads, int excl, int subclass_cnt) {
assert (job_class >= 1 && job_class <= JC_MAX);
assert (min_threads >= 0 && max_threads >= min_threads);
struct job_subclass_list *L = calloc (sizeof (*L), 1);
L->subclass_cnt = subclass_cnt;
L->subclasses = calloc (sizeof (struct job_subclass), subclass_cnt + 2);
L->subclasses += 2;
int i;
for (i = -2; i < subclass_cnt; i++) {
L->subclasses[i].job_queue = alloc_mp_queue_w ();
L->subclasses[i].subclass_id = i;
}
for (i = 0; i < MAX_SUBCLASS_THREADS; i++) {
sem_post (&L->sem);
}
JobClasses[job_class].subclasses = L;
return create_job_class (job_class, min_threads, max_threads, excl);
}
/* ------ JOB THREAD CODE -------- */
int try_lock_job (job_t job, int set_flags, int clear_flags) {
while (1) {
barrier ();
int flags = job->j_flags;
if (flags & JF_LOCKED) {
return 0;
}
if (__sync_bool_compare_and_swap (&job->j_flags, flags, (flags & ~clear_flags) | set_flags | JF_LOCKED)) {
job->j_thread = this_job_thread;
return 1;
}
}
}
int unlock_job (JOB_REF_ARG (job)) {
assert (job->j_thread == this_job_thread);
struct job_thread *JT = job->j_thread;
int thread_class = JT->thread_class;
int save_subclass = job->j_subclass;
vkprintf (JOBS_DEBUG, "UNLOCK JOB %p, type %p, flags %08x, status %08x, sigclass %08x, refcnt %d\n", job, job->j_execute, job->j_flags, job->j_status, job->j_sigclass, job->j_refcnt);
while (1) {
barrier ();
assert (job->j_flags & JF_LOCKED);
int flags = job->j_flags;
int todo = flags & job->j_status & (-1 << 24);
if (!todo) /* {{{ */ {
int new_flags = flags & ~JF_LOCKED;
if (!__sync_bool_compare_and_swap (&job->j_flags, flags, new_flags)) {
continue;
}
if (job->j_refcnt >= 2) {
if (__sync_fetch_and_add (&job->j_refcnt, -1) != 1) {
return 0;
}
job->j_refcnt = 1;
}
assert (job->j_refcnt == 1);
vkprintf (JOBS_DEBUG, "DESTROYING JOB %p, type %p, flags %08x\n", job, job->j_execute, job->j_flags);
if (job->j_status & JSS_ALLOW (JS_FINISH)) {
// send signal 7 (JS_FINISH) if it is allowed
job->j_flags |= JFS_SET (JS_FINISH) | JF_LOCKED;
continue;
} else {
assert (0 && "unhandled JS_FINISH\n");
MODULE_STAT->jobs_allocated_memory -= sizeof (struct async_job) + job->j_custom_bytes;
// complete_job (job);
job_free (JOB_REF_PASS (job)); // ???
JT->jobs_active --;
return -1;
}
}
/* }}} */
int signo = 7 - __builtin_clz (todo);
int req_class = (job->j_sigclass >> (signo*4)) & 15;
int is_fast = job->j_status & JSS_FAST (signo);
int cur_subclass = job->j_subclass;
/* {{{ Try to run signal signo */
if (((JT->job_class_mask >> req_class) & 1) && (is_fast || !JT->current_job) && (cur_subclass == save_subclass)) {
job_t current_job = JT->current_job;
__sync_fetch_and_and (&job->j_flags, ~JFS_SET (signo));
JT->jobs_running[req_class] ++;
JT->current_job = job;
JT->status |= JTS_PERFORMING;
vkprintf (JOBS_DEBUG, "BEGIN JOB %p, type %p, flags %08x, status %08x, sigclass %08x (signal %d of class %d), refcnt %d\n", job, job->j_execute, job->j_flags, job->j_status, job->j_sigclass, signo, req_class, job->j_refcnt);
int custom = job->j_custom_bytes;
int res = job->j_execute (job, signo, JT);
JT->current_job = current_job;
if (!current_job) {
JT->status &= ~JTS_PERFORMING;
}
JT->jobs_running[req_class] --;
if (res == JOB_DESTROYED) {
MODULE_STAT->jobs_allocated_memory -= sizeof (struct async_job) + custom;
vkprintf (JOBS_DEBUG, "JOB %p DESTROYED: RES = %d\n", job, res);
JT->jobs_active --;
return res;
}
vkprintf (JOBS_DEBUG, "END JOB %p, type %p, flags %08x, status %08x, sigclass %08x (signal %d of class %d), refcnt %d, %d children: RES = %d\n", job, job->j_execute, job->j_flags, job->j_status, job->j_sigclass, signo, req_class, job->j_refcnt, job->j_children, res);
if (res == JOB_ERROR) {
kprintf ("fatal: thread %p of class %d: error while invoking method %d of job %p (type %p)\n", JT, thread_class, signo, job, job->j_execute);
assert (0 && "unknown job signal");
}
if (!(res & ~0x1ff)) {
if (res & 0xff) {
__sync_fetch_and_or (&job->j_flags, res << 24);
}
if (res & JOB_COMPLETED) {
complete_job (job);
}
}
continue;
}
/* }}} */
/* {{{ Try to Queue */
if (!req_class) {
// have a "fast" signal with *-class, put it into MAIN queue
req_class = JC_MAIN;
}
// have to insert job into queue of req_class
int queued_flag = JF_QUEUED_CLASS (req_class);
int new_flags = (flags | queued_flag) & ~JF_LOCKED;
if (!__sync_bool_compare_and_swap (&job->j_flags, flags, new_flags)) {
continue;
}
if (!(flags & queued_flag)) {
struct job_class *JC = &JobClasses[req_class];
if (!JC->subclasses) {
struct mp_queue *JQ = JC->job_queue;
assert (JQ);
vkprintf (JOBS_DEBUG, "RESCHEDULED JOB %p, type %p, flags %08x, refcnt %d -> Queue %d\n", job, job->j_execute, job->j_flags, job->j_refcnt, req_class);
vkprintf (JOBS_DEBUG, "sub=%p\n", JT->job_class->subclasses);
mpq_push_w (JQ, PTR_MOVE (job), 0);
if (JQ == &MainJobQueue && main_thread_interrupt_status == 1 && __sync_fetch_and_add (&main_thread_interrupt_status, 1) == 1) {
//pthread_kill (main_pthread_id, SIGRTMAX - 7);
vkprintf (JOBS_DEBUG, "WAKING UP MAIN THREAD\n");
wakeup_main_thread ();
}
} else {
assert (job->j_subclass == cur_subclass);
assert (cur_subclass >= -2);
assert (cur_subclass < JC->subclasses->subclass_cnt);
struct job_subclass *JSC = &JC->subclasses->subclasses[cur_subclass];
__sync_fetch_and_add (&JSC->total_jobs, 1);
vkprintf (JOBS_DEBUG, "RESCHEDULED JOB %p, type %p, flags %08x, refcnt %d -> Queue %d subclass %d\n", job, job->j_execute, job->j_flags, job->j_refcnt, req_class, cur_subclass);
mpq_push_w (JSC->job_queue, PTR_MOVE (job), 0);
struct mp_queue *JQ = JC->job_queue;
assert (JQ);
mpq_push_w (JQ, (void *)(long)(cur_subclass + JOB_SUBCLASS_OFFSET), 0);
}
return 1;
} else {
job_decref (JOB_REF_PASS (job));
return 0;
}
/* }}} */
}
}
// destroys one reference to job; sends signal signo to it
void job_send_signals (JOB_REF_ARG (job), int sigset) {
vkprintf (JOBS_DEBUG, "SENDING SIGNALS %08x to JOB %p, type %p, flags %08x, refcnt %d\n", sigset, job, job->j_execute, job->j_flags, job->j_refcnt);
assert (!(sigset & 0xffffff));
assert (job->j_refcnt > 0);
if ((job->j_flags & sigset) == sigset) {
assert (job->j_refcnt > 1 || !(job->j_flags & JFS_SET (JS_FINISH)));
job_decref (JOB_REF_PASS (job));
return;
}
if (try_lock_job (job, sigset, 0)) {
unlock_job (JOB_REF_PASS (job));
return;
}
__sync_fetch_and_or (&job->j_flags, sigset);
if (try_lock_job (job, 0, 0)) {
unlock_job (JOB_REF_PASS (job));
} else {
if (job->j_flags & JF_SIGINT) {
assert (job->j_thread);
pthread_kill (job->j_thread->pthread_id, SIGRTMAX - 7);
}
job_decref (JOB_REF_PASS (job));
}
}
// destroys one reference to job; sends signal signo to it
void job_signal (JOB_REF_ARG (job), int signo) {
assert ((unsigned) signo <= 7);
job_send_signals (JOB_REF_PASS (job), JFS_SET (signo));
}
// destroys one reference to job
void job_decref (JOB_REF_ARG (job)) {
if (job->j_refcnt >= 2) {
if (__sync_fetch_and_add (&job->j_refcnt, -1) != 1) {
return;
}
job->j_refcnt = 1;
}
assert (job->j_refcnt == 1);
job_signal (JOB_REF_PASS (job), JS_FINISH);
}
// creates one reference to job
job_t job_incref (job_t job) {
//if (job->j_refcnt == 1) {
// job->j_refcnt = 2;
//} else {
__sync_fetch_and_add (&job->j_refcnt, 1);
//}
return job;
}
void process_one_job (JOB_REF_ARG (job), int thread_class) {
struct job_thread *JT = this_job_thread;
assert (JT);
assert (job);
int queued_flag = job->j_flags & 0xffff & JT->job_class_mask;
if (try_lock_job (job, 0, queued_flag)) {
unlock_job (JOB_REF_PASS (job));
} else {
__sync_fetch_and_and (&job->j_flags, ~queued_flag);
if (try_lock_job (job, 0, 0)) {
unlock_job (JOB_REF_PASS (job));
} else {
job_decref (JOB_REF_PASS (job));
}
}
}
void complete_subjob (job_t job, JOB_REF_ARG (parent), int status) {
if (!parent) {
return;
}
if (parent->j_flags & JF_COMPLETED) {
job_decref (JOB_REF_PASS (parent));
return;
}
if (job->j_error && (status & JSP_PARENT_ERROR)) {
if (!parent->j_error) {
__sync_bool_compare_and_swap (&parent->j_error, 0, job->j_error);
}
if (status & JSP_PARENT_WAKEUP) {
__sync_fetch_and_add (&parent->j_children, -1);
}
vkprintf (JOBS_DEBUG, "waking up parent %p with JS_ABORT (%d children remaining)\n", parent, parent->j_children);
job_signal (JOB_REF_PASS (parent), JS_ABORT);
return;
}
if (status & JSP_PARENT_WAKEUP) {
if (__sync_fetch_and_add (&parent->j_children, -1) == 1 && (status & JSP_PARENT_RUN)) {
vkprintf (JOBS_DEBUG, "waking up parent %p with JS_RUN\n", parent);
job_signal (JOB_REF_PASS (parent), JS_RUN);
} else {
vkprintf (JOBS_DEBUG, "parent %p: %d children remaining\n", parent, parent->j_children);
job_decref (JOB_REF_PASS (parent));
}
return;
}
if (status & JSP_PARENT_RUN) {
job_signal (JOB_REF_PASS (parent), JS_RUN);
return;
}
job_decref (JOB_REF_PASS (parent));
}
void complete_job (job_t job) {
vkprintf (JOBS_DEBUG, "COMPLETE JOB %p, type %p, flags %08x, status %08x, error %d; refcnt=%d; PARENT %p\n", job, job->j_execute, job->j_flags, job->j_status, job->j_error, job->j_refcnt, job->j_parent);
assert (job->j_flags & JF_LOCKED);
if (job->j_flags & JF_COMPLETED) {
return;
}
__sync_fetch_and_or (&job->j_flags, JF_COMPLETED);
job_t parent = PTR_MOVE (job->j_parent);
if (!parent) {
return;
}
complete_subjob (job, JOB_REF_PASS (parent), job->j_status);
}
static void job_interrupt_signal_handler (const int sig) {
char buffer[256];
if (verbosity >= 2) {
kwrite (2, buffer, sprintf (buffer, "SIGRTMAX-7 (JOB INTERRUPT) caught in thread #%d running job %p.\n", this_job_thread ? this_job_thread->id : -1, this_job_thread ? this_job_thread->current_job : 0));
}
}
static void set_job_interrupt_signal_handler (void) {
struct sigaction act;
sigemptyset (&act.sa_mask);
act.sa_flags = 0;
act.sa_handler = job_interrupt_signal_handler;
if (sigaction (SIGRTMAX - 7, &act, NULL) != 0) {
kwrite (2, "failed sigaction\n", 17);
_exit (EXIT_FAILURE);
}
}
void *job_thread_ex (void *arg, void (*work_one)(void *, int)) {
struct job_thread *JT = arg;
this_job_thread = JT;
assert (JT->thread_class);
assert (!(JT->thread_class & ~JC_MASK));
get_this_thread_id ();
JT->thread_system_id = syscall (SYS_gettid);
set_job_interrupt_signal_handler ();
struct thread_callback *cb = jobs_cb_list;
while (cb) {
cb->new_thread ();
cb = cb->next;
}
JT->status |= JTS_RUNNING;
int thread_class = JT->thread_class;
struct mp_queue *Q = JT->job_queue;
// void **hptr = thread_hazard_pointers;
if (JT->job_class->max_threads == 1) {
JT->timer_manager = alloc_timer_manager (thread_class);
}
int prev_now = 0;
long long last_rdtsc = 0;
while (1) {
void *job = mpq_pop_nw (Q, 4);
if (!job) {
double wait_start = get_utime_monotonic ();
MODULE_STAT->locked_since = wait_start;
job = mpq_pop_w (Q, 4);
double wait_time = get_utime_monotonic () - wait_start;
MODULE_STAT->locked_since = 0;
MODULE_STAT->tot_idle_time += wait_time;
MODULE_STAT->a_idle_time += wait_time;
}
long long new_rdtsc = rdtsc ();
if (new_rdtsc - last_rdtsc > 1000000) {
get_utime_monotonic ();
now = time (0);
if (now > prev_now && now < prev_now + 60) {
while (prev_now < now) {
MODULE_STAT->a_idle_time *= 100.0 / 101;
MODULE_STAT->a_idle_quotient = a_idle_quotient * (100.0/101) + 1;
prev_now++;
}
} else {
if (now >= prev_now + 60) {
MODULE_STAT->a_idle_time = MODULE_STAT->a_idle_quotient;
}
prev_now = now;
}
last_rdtsc = new_rdtsc;
}
vkprintf (JOBS_DEBUG, "JOB THREAD #%d (CLASS %d): got job %p\n", JT->id, thread_class, job);
work_one (PTR_MOVE (job), thread_class);
}
pthread_exit (0);
}
static void process_one_sublist (unsigned long id, int class) {
struct job_thread *JT = this_job_thread;
assert (JT);
struct job_class *JC = JT->job_class;
assert (JC->subclasses);
struct job_subclass_list *J_SCL = JC->subclasses;
id -= JOB_SUBCLASS_OFFSET;
int subclass_id = id;
assert (subclass_id >= -2);
assert (subclass_id < JC->subclasses->subclass_cnt);
struct job_subclass *J_SC = &J_SCL->subclasses[subclass_id];
__sync_fetch_and_add (&J_SC->allowed_to_run_jobs, 1);
if (!__sync_bool_compare_and_swap (&J_SC->locked, 0, 1)) {
return;
}
if (subclass_id != -1) {
while (sem_wait (&J_SCL->sem) < 0);
} else {
int i;
for (i = 0; i < MAX_SUBCLASS_THREADS; i++) {
while (sem_wait (&J_SCL->sem));
}
}
while (1) {
while (J_SC->processed_jobs < J_SC->allowed_to_run_jobs) {
job_t job = mpq_pop_nw (J_SC->job_queue, 4);
assert (job);
process_one_job (JOB_REF_PASS (job), JT->thread_class);
J_SC->processed_jobs ++;
}
J_SC->locked = 0;
__sync_synchronize ();
if (J_SC->processed_jobs < J_SC->allowed_to_run_jobs &&
__sync_bool_compare_and_swap (&J_SC->locked, 0, 1)) {