File: proc_cpuview.c

package info (click to toggle)
lxcfs 4.0.7-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 2,284 kB
  • sloc: ansic: 9,684; sh: 4,739; makefile: 194
file content (1194 lines) | stat: -rw-r--r-- 28,870 bytes parent folder | download
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
/* SPDX-License-Identifier: LGPL-2.1+ */

#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif

#include "config.h"

#ifdef HAVE_FUSE3
#ifndef FUSE_USE_VERSION
#define FUSE_USE_VERSION 30
#endif
#else
#ifndef FUSE_USE_VERSION
#define FUSE_USE_VERSION 26
#endif
#endif

#define _FILE_OFFSET_BITS 64

#define __STDC_FORMAT_MACROS
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <fuse.h>
#include <inttypes.h>
#include <libgen.h>
#include <pthread.h>
#include <sched.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <wait.h>
#include <linux/magic.h>
#include <linux/sched.h>
#include <sys/epoll.h>
#include <sys/mman.h>
#include <sys/mount.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <sys/syscall.h>
#include <sys/sysinfo.h>
#include <sys/vfs.h>

#include "bindings.h"
#include "cgroup_fuse.h"
#include "cpuset_parse.h"
#include "cgroups/cgroup.h"
#include "cgroups/cgroup_utils.h"
#include "memory_utils.h"
#include "proc_loadavg.h"
#include "utils.h"

/* Data for CPU view */
struct cg_proc_stat {
	char *cg;
	struct cpuacct_usage *usage; 	/* Real usage as read from the host's /proc/stat. */
	struct cpuacct_usage *view; 	/* Usage stats reported to the container. */
	int cpu_count;
	pthread_mutex_t lock; 		/* For node manipulation. */
	struct cg_proc_stat *next;
};

struct cg_proc_stat_head {
	struct cg_proc_stat *next;
	time_t lastcheck;

	/*
	 * For access to the list. Reading can be parallel, pruning is exclusive.
	 */
	pthread_rwlock_t lock;
};

#define CPUVIEW_HASH_SIZE 100
static struct cg_proc_stat_head *proc_stat_history[CPUVIEW_HASH_SIZE];

static void reset_proc_stat_node(struct cg_proc_stat *node,
				 struct cpuacct_usage *usage, int cpu_count)
{
	lxcfs_debug("Resetting stat node for %s\n", node->cg);
	memcpy(node->usage, usage, sizeof(struct cpuacct_usage) * cpu_count);

	for (int i = 0; i < cpu_count; i++) {
		node->view[i].user = 0;
		node->view[i].system = 0;
		node->view[i].idle = 0;
	}

	node->cpu_count = cpu_count;
}

static bool expand_proc_stat_node(struct cg_proc_stat *node, int cpu_count)
{
	__do_free struct cpuacct_usage *new_usage = NULL, *new_view = NULL;

	/* Allocate new memory */
	new_usage = zalloc(sizeof(struct cpuacct_usage) * cpu_count);
	if (!new_usage)
		return false;

	new_view = zalloc(sizeof(struct cpuacct_usage) * cpu_count);
	if (!new_view)
		return false;

	/* Copy existing data & initialize new elements */
	for (int i = 0; i < cpu_count; i++) {
		if (i < node->cpu_count) {
			new_usage[i].user 	= node->usage[i].user;
			new_usage[i].system 	= node->usage[i].system;
			new_usage[i].idle 	= node->usage[i].idle;

			new_view[i].user 	= node->view[i].user;
			new_view[i].system 	= node->view[i].system;
			new_view[i].idle 	= node->view[i].idle;
		}
	}

	free(node->usage);
	node->usage = move_ptr(new_usage);

	free(node->view);
	node->view = move_ptr(new_view);
	node->cpu_count = cpu_count;

	return true;
}

static void free_proc_stat_node(struct cg_proc_stat *node)
{
	if (node) {
		/*
		 * We're abusing the usage pointer to indicate that
		 * pthread_mutex_init() was successful. Don't judge me.
		 */
		if (node->usage)
			pthread_mutex_destroy(&node->lock);
		free_disarm(node->cg);
		free_disarm(node->usage);
		free_disarm(node->view);
		free_disarm(node);
	}
}

define_cleanup_function(struct cg_proc_stat *, free_proc_stat_node);

static struct cg_proc_stat *add_proc_stat_node(struct cg_proc_stat *new_node)
{
	call_cleaner(free_proc_stat_node) struct cg_proc_stat *new = new_node;
	struct cg_proc_stat *rv = new_node;
	int hash = calc_hash(new->cg) % CPUVIEW_HASH_SIZE;
	struct cg_proc_stat_head *head = proc_stat_history[hash];
	struct cg_proc_stat *cur;

	pthread_rwlock_wrlock(&head->lock);

	if (!head->next) {
		head->next = move_ptr(new);
		goto out_rwlock_unlock;
	}

	cur = head->next;

	for (;;) {
		/*
		 * The node to be added is already present in the list, so
		 * free the newly allocated one and return the one we found.
		 */
		if (strcmp(cur->cg, new->cg) == 0) {
			rv = cur;
			goto out_rwlock_unlock;
		}

		/* Keep walking. */
		if (cur->next) {
			cur = cur->next;
			continue;
		}

		/* Add new node to end of list. */
		cur->next = move_ptr(new);
		goto out_rwlock_unlock;
	}

out_rwlock_unlock:
	pthread_rwlock_unlock(&head->lock);
	return move_ptr(rv);
}

static struct cg_proc_stat *new_proc_stat_node(struct cpuacct_usage *usage,
					       int cpu_count, const char *cg)
{
	call_cleaner(free_proc_stat_node) struct cg_proc_stat *node = NULL;
	__do_free struct cpuacct_usage *new_usage = NULL;

	node = zalloc(sizeof(struct cg_proc_stat));
	if (!node)
		return NULL;

	node->cg = strdup(cg);
	if (!node->cg)
		return NULL;

	new_usage = memdup(usage, sizeof(struct cpuacct_usage) * cpu_count);
	if (!new_usage)
		return NULL;

	node->view = zalloc(sizeof(struct cpuacct_usage) * cpu_count);
	if (!node->view)
		return NULL;

	node->cpu_count = cpu_count;

	if (pthread_mutex_init(&node->lock, NULL))
		return NULL;
	/*
	 * We're abusing the usage pointer to indicate that
	 * pthread_mutex_init() was successful. Don't judge me.
	 */
	node->usage = move_ptr(new_usage);

	return move_ptr(node);
}

static bool cgroup_supports(const char *controller, const char *cgroup,
			    const char *file)
{
	__do_free char *path = NULL;
	int cfd;

	cfd = get_cgroup_fd(controller);
	if (cfd < 0)
		return false;

	path = must_make_path_relative(cgroup, file, NULL);
	return faccessat(cfd, path, F_OK, 0) == 0;
}

static struct cg_proc_stat *prune_proc_stat_list(struct cg_proc_stat *node)
{
	struct cg_proc_stat *first = NULL;

	for (struct cg_proc_stat *prev = NULL; node; ) {
		if (!cgroup_supports("cpu", node->cg, "cpu.shares")) {
			call_cleaner(free_proc_stat_node) struct cg_proc_stat *cur = node;

			if (prev)
				prev->next = node->next;
			else
				first = node->next;

			node = node->next;
			lxcfs_debug("Removing stat node for %s\n", cur->cg);
		} else {
			if (!first)
				first = node;
			prev = node;
			node = node->next;
		}
	}

	return first;
}

#define PROC_STAT_PRUNE_INTERVAL 10
static void prune_proc_stat_history(void)
{
	time_t now = time(NULL);

	for (int i = 0; i < CPUVIEW_HASH_SIZE; i++) {
		pthread_rwlock_wrlock(&proc_stat_history[i]->lock);

		if ((proc_stat_history[i]->lastcheck + PROC_STAT_PRUNE_INTERVAL) > now) {
			pthread_rwlock_unlock(&proc_stat_history[i]->lock);
			return;
		}

		if (proc_stat_history[i]->next) {
			proc_stat_history[i]->next = prune_proc_stat_list(proc_stat_history[i]->next);
			proc_stat_history[i]->lastcheck = now;
		}

		pthread_rwlock_unlock(&proc_stat_history[i]->lock);
	}
}

static struct cg_proc_stat *find_proc_stat_node(struct cg_proc_stat_head *head,
						const char *cg)
{
	struct cg_proc_stat *node;

	pthread_rwlock_rdlock(&head->lock);

	if (!head->next) {
		pthread_rwlock_unlock(&head->lock);
		return NULL;
	}

	node = head->next;

	do {
		if (strcmp(cg, node->cg) == 0)
			goto out;
	} while ((node = node->next));

	node = NULL;

out:
	pthread_rwlock_unlock(&head->lock);
	prune_proc_stat_history();
	return node;
}

static struct cg_proc_stat *find_or_create_proc_stat_node(struct cpuacct_usage *usage,
							  int cpu_count, const char *cg)
{
	int hash = calc_hash(cg) % CPUVIEW_HASH_SIZE;
	struct cg_proc_stat_head *head = proc_stat_history[hash];
	struct cg_proc_stat *node;

	node = find_proc_stat_node(head, cg);
	if (!node) {
		node = new_proc_stat_node(usage, cpu_count, cg);
		if (!node)
			return NULL;

		node = add_proc_stat_node(node);
		lxcfs_debug("New stat node (%d) for %s\n", cpu_count, cg);
	}

	pthread_mutex_lock(&node->lock);

	/*
	 * If additional CPUs on the host have been enabled, CPU usage counter
	 * arrays have to be expanded.
	 */
	if (node->cpu_count < cpu_count) {
		lxcfs_debug("Expanding stat node %d->%d for %s\n",
			    node->cpu_count, cpu_count, cg);

		if (!expand_proc_stat_node(node, cpu_count)) {
			pthread_mutex_unlock(&node->lock);
			return log_debug(NULL, "Unable to expand stat node %d->%d for %s", node->cpu_count, cpu_count, cg);
		}
	}

	return node;
}

static void add_cpu_usage(uint64_t *surplus, struct cpuacct_usage *usage,
			  uint64_t *counter, uint64_t threshold)
{
	uint64_t free_space, to_add;

	free_space = threshold - usage->user - usage->system;

	if (free_space > usage->idle)
		free_space = usage->idle;

	if (free_space > *surplus)
		to_add = *surplus;
	else
		to_add = free_space;

	*counter += to_add;
	usage->idle -= to_add;
	*surplus -= to_add;
}

static uint64_t diff_cpu_usage(struct cpuacct_usage *older,
			       struct cpuacct_usage *newer,
			       struct cpuacct_usage *diff, int cpu_count)
{
	uint64_t sum = 0;

	for (int i = 0; i < cpu_count; i++) {
		if (!newer[i].online)
			continue;

		/*
		 * When cpuset is changed on the fly, the CPUs might get
		 * reordered. We could either reset all counters, or check
		 * that the substractions below will return expected results.
		 */
		if (newer[i].user > older[i].user)
			diff[i].user = newer[i].user - older[i].user;
		else
			diff[i].user = 0;

		if (newer[i].system > older[i].system)
			diff[i].system = newer[i].system - older[i].system;
		else
			diff[i].system = 0;

		if (newer[i].idle > older[i].idle)
			diff[i].idle = newer[i].idle - older[i].idle;
		else
			diff[i].idle = 0;

		sum += diff[i].user;
		sum += diff[i].system;
		sum += diff[i].idle;
	}

	return sum;
}

/*
 * Read cgroup CPU quota parameters from `cpu.cfs_quota_us` or
 * `cpu.cfs_period_us`, depending on `param`. Parameter value is returned
 * through `value`.
 */
static bool read_cpu_cfs_param(const char *cg, const char *param, int64_t *value)
{
	__do_free char *str = NULL;
	char file[STRLITERALLEN("cpu.cfs_period_us") + 1];
	bool first = true;
	int ret;

	if (pure_unified_layout(cgroup_ops)) {
		first = !strcmp(param, "quota");
		ret = snprintf(file, sizeof(file), "cpu.max");
	} else {
		ret = snprintf(file, sizeof(file), "cpu.cfs_%s_us", param);
	}
	if (ret < 0 || (size_t)ret >= sizeof(file))
		return false;

	if (!cgroup_ops->get(cgroup_ops, "cpu", cg, file, &str))
		return false;

	return sscanf(str, first ? "%" PRId64 : "%*d %" PRId64, value) == 1;
}

/*
 * Return the exact number of visible CPUs based on CPU quotas.
 * If there is no quota set, zero is returned.
 */
static double exact_cpu_count(const char *cg)
{
	double rv;
	int nprocs;
	int64_t cfs_quota, cfs_period;

	if (!read_cpu_cfs_param(cg, "quota", &cfs_quota))
		return 0;

	if (!read_cpu_cfs_param(cg, "period", &cfs_period))
		return 0;

	if (cfs_quota <= 0 || cfs_period <= 0)
		return 0;

	rv = (double)cfs_quota / (double)cfs_period;

	nprocs = get_nprocs();

	if (rv > nprocs)
		rv = nprocs;

	return rv;
}

/*
 * Return the maximum number of visible CPUs based on CPU quotas.
 * If there is no quota set, zero is returned.
 */
int max_cpu_count(const char *cg)
{
	__do_free char *cpuset = NULL;
	int rv, nprocs;
	int64_t cfs_quota, cfs_period;
	int nr_cpus_in_cpuset = 0;

	if (!read_cpu_cfs_param(cg, "quota", &cfs_quota))
		return 0;

	if (!read_cpu_cfs_param(cg, "period", &cfs_period))
		return 0;

	cpuset = get_cpuset(cg);
	if (cpuset)
		nr_cpus_in_cpuset = cpu_number_in_cpuset(cpuset);

	if (cfs_quota <= 0 || cfs_period <= 0) {
		if (nr_cpus_in_cpuset > 0)
			return nr_cpus_in_cpuset;

		return 0;
	}

	rv = cfs_quota / cfs_period;

	/*
	 * In case quota/period does not yield a whole number, add one CPU for
	 * the remainder.
	 */
	if ((cfs_quota % cfs_period) > 0)
		rv += 1;

	nprocs = get_nprocs();
	if (rv > nprocs)
		rv = nprocs;

	/* Use min value in cpu quota and cpuset. */
	if (nr_cpus_in_cpuset > 0 && nr_cpus_in_cpuset < rv)
		rv = nr_cpus_in_cpuset;

	return rv;
}

int cpuview_proc_stat(const char *cg, const char *cpuset,
		      struct cpuacct_usage *cg_cpu_usage, int cg_cpu_usage_size,
		      FILE *f, char *buf, size_t buf_size)
{
	__do_free char *line = NULL;
	__do_free struct cpuacct_usage *diff = NULL;
	size_t linelen = 0, total_len = 0;
	int curcpu = -1; /* cpu numbering starts at 0 */
	int physcpu, i;
	int cpu_cnt = 0;
	uint64_t user = 0, nice = 0, system = 0, idle = 0, iowait = 0, irq = 0,
		 softirq = 0, steal = 0, guest = 0, guest_nice = 0;
	uint64_t user_sum = 0, system_sum = 0, idle_sum = 0;
	uint64_t user_surplus = 0, system_surplus = 0;
	int nprocs, max_cpus;
	ssize_t l;
	uint64_t total_sum, threshold;
	struct cg_proc_stat *stat_node;

	nprocs = get_nprocs_conf();
	if (cg_cpu_usage_size < nprocs)
		nprocs = cg_cpu_usage_size;

	/* Read all CPU stats and stop when we've encountered other lines */
	while (getline(&line, &linelen, f) != -1) {
		int ret;
		char cpu_char[10]; /* That's a lot of cores */
		uint64_t all_used, cg_used;

		if (strlen(line) == 0)
			continue;

		/* not a ^cpuN line containing a number N */
		if (sscanf(line, "cpu%9[^ ]", cpu_char) != 1)
			break;

		if (sscanf(cpu_char, "%d", &physcpu) != 1)
			continue;

		if (physcpu >= cg_cpu_usage_size)
			continue;

		curcpu++;
		cpu_cnt++;

		if (!cpu_in_cpuset(physcpu, cpuset)) {
			for (i = curcpu; i <= physcpu; i++)
				cg_cpu_usage[i].online = false;
			continue;
		}

		if (curcpu < physcpu) {
			/* Some CPUs may be disabled */
			for (i = curcpu; i < physcpu; i++)
				cg_cpu_usage[i].online = false;

			curcpu = physcpu;
		}

		cg_cpu_usage[curcpu].online = true;

		ret = sscanf(line, "%*s %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 "lu",
			   &user,
			   &nice,
			   &system,
			   &idle,
			   &iowait,
			   &irq,
			   &softirq,
			   &steal,
			   &guest,
			   &guest_nice);
		if (ret != 10)
			continue;

		all_used = user + nice + system + iowait + irq + softirq + steal + guest + guest_nice;
		cg_used = cg_cpu_usage[curcpu].user + cg_cpu_usage[curcpu].system;

		if (all_used >= cg_used) {
			cg_cpu_usage[curcpu].idle = idle + (all_used - cg_used);

		} else {
			lxcfs_error("cpu%d from %s has unexpected cpu time: %" PRIu64 " in /proc/stat, %" PRIu64 " in cpuacct.usage_all; unable to determine idle time",
				    curcpu, cg, all_used, cg_used);
			cg_cpu_usage[curcpu].idle = idle;
		}
	}

	/* Cannot use more CPUs than is available in cpuset. */
	max_cpus = max_cpu_count(cg);
	if (max_cpus > cpu_cnt || !max_cpus)
		max_cpus = cpu_cnt;

	/* takes lock pthread_mutex_lock(&node->lock) */
	stat_node = find_or_create_proc_stat_node(cg_cpu_usage, nprocs, cg);
	if (!stat_node)
		return log_error(0, "Failed to find/create stat node for %s", cg);

	diff = zalloc(sizeof(struct cpuacct_usage) * nprocs);
	if (!diff)
		return 0;

	/*
	 * If the new values are LOWER than values stored in memory, it means
	 * the cgroup has been reset/recreated and we should reset too.
	 */
	for (curcpu = 0; curcpu < nprocs; curcpu++) {
		if (!cg_cpu_usage[curcpu].online)
			continue;

		if (cg_cpu_usage[curcpu].user < stat_node->usage[curcpu].user)
			reset_proc_stat_node(stat_node, cg_cpu_usage, nprocs);

		break;
	}

	total_sum = diff_cpu_usage(stat_node->usage, cg_cpu_usage, diff, nprocs);

	for (curcpu = 0, i = -1; curcpu < nprocs; curcpu++) {
		stat_node->usage[curcpu].online = cg_cpu_usage[curcpu].online;

		if (!stat_node->usage[curcpu].online)
			continue;

		i++;

		stat_node->usage[curcpu].user 	+= diff[curcpu].user;
		stat_node->usage[curcpu].system += diff[curcpu].system;
		stat_node->usage[curcpu].idle 	+= diff[curcpu].idle;

		if (max_cpus > 0 && i >= max_cpus) {
			user_surplus 	+= diff[curcpu].user;
			system_surplus 	+= diff[curcpu].system;
		}
	}

	/* Calculate usage counters of visible CPUs */
	if (max_cpus > 0) {
		uint64_t diff_user = 0;
		uint64_t diff_system = 0;
		uint64_t diff_idle = 0;
		uint64_t max_diff_idle = 0;
		uint64_t max_diff_idle_index = 0;
		double exact_cpus;

		/* threshold = maximum usage per cpu, including idle */
		threshold = total_sum / cpu_cnt * max_cpus;

		for (curcpu = 0, i = -1; curcpu < nprocs; curcpu++) {
			if (!stat_node->usage[curcpu].online)
				continue;

			i++;

			if (i == max_cpus)
				break;

			if (diff[curcpu].user + diff[curcpu].system >= threshold)
				continue;

			/* Add user */
			add_cpu_usage(&user_surplus, &diff[curcpu],
				      &diff[curcpu].user, threshold);

			if (diff[curcpu].user + diff[curcpu].system >= threshold)
				continue;

			/* If there is still room, add system */
			add_cpu_usage(&system_surplus, &diff[curcpu],
				      &diff[curcpu].system, threshold);
		}

		if (user_surplus > 0)
			lxcfs_debug("leftover user: %lu for %s\n", user_surplus, cg);
		if (system_surplus > 0)
			lxcfs_debug("leftover system: %lu for %s\n", system_surplus, cg);

		for (curcpu = 0, i = -1; curcpu < nprocs; curcpu++) {
			if (!stat_node->usage[curcpu].online)
				continue;

			i++;

			if (i == max_cpus)
				break;

			stat_node->view[curcpu].user 	+= diff[curcpu].user;
			stat_node->view[curcpu].system 	+= diff[curcpu].system;
			stat_node->view[curcpu].idle 	+= diff[curcpu].idle;

			user_sum 	+= stat_node->view[curcpu].user;
			system_sum 	+= stat_node->view[curcpu].system;
			idle_sum 	+= stat_node->view[curcpu].idle;

			diff_user 	+= diff[curcpu].user;
			diff_system 	+= diff[curcpu].system;
			diff_idle 	+= diff[curcpu].idle;
			if (diff[curcpu].idle > max_diff_idle) {
				max_diff_idle 		= diff[curcpu].idle;
				max_diff_idle_index 	= curcpu;
			}

			lxcfs_v("curcpu: %d, diff_user: %lu, diff_system: %lu, diff_idle: %lu\n", curcpu, diff[curcpu].user, diff[curcpu].system, diff[curcpu].idle);
		}
		lxcfs_v("total. diff_user: %lu, diff_system: %lu, diff_idle: %lu\n", diff_user, diff_system, diff_idle);

		/* revise cpu usage view to support partial cpu case. */
		exact_cpus = exact_cpu_count(cg);
		if (exact_cpus < (double)max_cpus){
			uint64_t delta = (uint64_t)((double)(diff_user + diff_system + diff_idle) * (1 - exact_cpus / (double)max_cpus));

			lxcfs_v("revising cpu usage view to match the exact cpu count [%f]\n", exact_cpus);
			lxcfs_v("delta: %lu\n", delta);
			lxcfs_v("idle_sum before: %lu\n", idle_sum);
			if (idle_sum > delta)
				idle_sum = idle_sum - delta;
			else
				idle_sum = 0;
			lxcfs_v("idle_sum after: %lu\n", idle_sum);

			curcpu = max_diff_idle_index;
			lxcfs_v("curcpu: %d, idle before: %lu\n", curcpu, stat_node->view[curcpu].idle);
			if (stat_node->view[curcpu].idle > delta)
				stat_node->view[curcpu].idle = stat_node->view[curcpu].idle - delta;
			else
				stat_node->view[curcpu].idle = 0;
			lxcfs_v("curcpu: %d, idle after: %lu\n", curcpu, stat_node->view[curcpu].idle);
		}
	} else {
		for (curcpu = 0; curcpu < nprocs; curcpu++) {
			if (!stat_node->usage[curcpu].online)
				continue;

			stat_node->view[curcpu].user 	= stat_node->usage[curcpu].user;
			stat_node->view[curcpu].system 	= stat_node->usage[curcpu].system;
			stat_node->view[curcpu].idle 	= stat_node->usage[curcpu].idle;

			user_sum 	+= stat_node->view[curcpu].user;
			system_sum 	+= stat_node->view[curcpu].system;
			idle_sum 	+= stat_node->view[curcpu].idle;
		}
	}

	/* Render the file */
	/* cpu-all */
	l = snprintf(buf, buf_size,
		     "cpu  %" PRIu64 " 0 %" PRIu64 " %" PRIu64 " 0 0 0 0 0 0\n",
		     user_sum, system_sum, idle_sum);
	lxcfs_v("cpu-all: %s\n", buf);
	if (l < 0) {
		lxcfs_error("Failed to write cache");
		total_len = 0;
		goto out_pthread_mutex_unlock;
	}
	if (l >= buf_size)
		return log_error(0, "Write to cache was truncated");

	buf += l;
	buf_size -= l;
	total_len += l;

	/* Render visible CPUs */
	for (curcpu = 0, i = -1; curcpu < nprocs; curcpu++) {
		if (!stat_node->usage[curcpu].online)
			continue;

		i++;

		if (max_cpus > 0 && i == max_cpus)
			break;

		l = snprintf(buf, buf_size, "cpu%d %" PRIu64 " 0 %" PRIu64 " %" PRIu64 " 0 0 0 0 0 0\n",
			     i,
			     stat_node->view[curcpu].user,
			     stat_node->view[curcpu].system,
			     stat_node->view[curcpu].idle);
		lxcfs_v("cpu: %s\n", buf);
		if (l < 0) {
			lxcfs_error("Failed to write cache");
			total_len = 0;
			goto out_pthread_mutex_unlock;
		}
		if (l >= buf_size) {
			lxcfs_error("Write to cache was truncated");
			total_len = 0;
			goto out_pthread_mutex_unlock;
		}

		buf += l;
		buf_size -= l;
		total_len += l;
	}

	/* Pass the rest of /proc/stat, start with the last line read */
	l = snprintf(buf, buf_size, "%s", line);
	if (l < 0) {
		lxcfs_error("Failed to write cache");
		total_len = 0;
		goto out_pthread_mutex_unlock;
	}
	if (l >= buf_size) {
		lxcfs_error("Write to cache was truncated");
		total_len = 0;
		goto out_pthread_mutex_unlock;
	}

	buf += l;
	buf_size -= l;
	total_len += l;

	/* Pass the rest of the host's /proc/stat */
	while (getline(&line, &linelen, f) != -1) {
		l = snprintf(buf, buf_size, "%s", line);
		if (l < 0) {
			lxcfs_error("Failed to write cache");
			total_len = 0;
			goto out_pthread_mutex_unlock;
		}
		if (l >= buf_size) {
			lxcfs_error("Write to cache was truncated");
			total_len = 0;
			goto out_pthread_mutex_unlock;
		}

		buf += l;
		buf_size -= l;
		total_len += l;
	}

out_pthread_mutex_unlock:
	if (stat_node)
		pthread_mutex_unlock(&stat_node->lock);

	return total_len;
}

/*
 * check whether this is a '^processor" line in /proc/cpuinfo
 */
static inline bool is_processor_line(const char *line)
{
	int cpu;
	return sscanf(line, "processor       : %d", &cpu) == 1;
}

static inline bool cpuline_in_cpuset(const char *line, const char *cpuset)
{
	int cpu;

	if (sscanf(line, "processor       : %d", &cpu) == 1)
		return cpu_in_cpuset(cpu, cpuset);

	return false;
}

int proc_cpuinfo_read(char *buf, size_t size, off_t offset,
		      struct fuse_file_info *fi)
{
	__do_free char *cg = NULL, *cpuset = NULL, *line = NULL;
	__do_free void *fopen_cache = NULL;
	__do_fclose FILE *f = NULL;
	struct fuse_context *fc = fuse_get_context();
	struct lxcfs_opts *opts = (struct lxcfs_opts *)fc->private_data;
	struct file_info *d = INTTYPE_TO_PTR(fi->fh);
	size_t linelen = 0, total_len = 0;
	bool am_printing = false, firstline = true, is_s390x = false;
	int curcpu = -1, cpu, max_cpus = 0;
	bool use_view;
	char *cache = d->buf;
	size_t cache_size = d->buflen;

	if (offset) {
		int left;

		if (offset > d->size)
			return -EINVAL;

		if (!d->cached)
			return 0;

		left = d->size - offset;
		total_len = left > size ? size: left;
		memcpy(buf, cache + offset, total_len);

		return total_len;
	}

	pid_t initpid = lookup_initpid_in_store(fc->pid);
	if (initpid <= 1 || is_shared_pidns(initpid))
		initpid = fc->pid;

	cg = get_pid_cgroup(initpid, "cpuset");
	if (!cg)
		return read_file_fuse("proc/cpuinfo", buf, size, d);
	prune_init_slice(cg);

	cpuset = get_cpuset(cg);
	if (!cpuset)
		return 0;

	if (cgroup_ops->can_use_cpuview(cgroup_ops) && opts && opts->use_cfs)
		use_view = true;
	else
		use_view = false;
	if (use_view)
		max_cpus = max_cpu_count(cg);

	f = fopen_cached("/proc/cpuinfo", "re", &fopen_cache);
	if (!f)
		return 0;

	while (getline(&line, &linelen, f) != -1) {
		ssize_t l;
		if (firstline) {
			firstline = false;
			if (strstr(line, "IBM/S390") != NULL) {
				is_s390x = true;
				am_printing = true;
				continue;
			}
		}

		if (strncmp(line, "# processors:", 12) == 0)
			continue;

		if (is_processor_line(line)) {
			if (use_view && max_cpus > 0 && (curcpu + 1) == max_cpus)
				break;

			am_printing = cpuline_in_cpuset(line, cpuset);
			if (am_printing) {
				curcpu++;
				l = snprintf(cache, cache_size, "processor	: %d\n", curcpu);
				if (l < 0)
					return log_error(0, "Failed to write cache");
				if (l >= cache_size)
					return log_error(0, "Write to cache was truncated");
				cache += l;
				cache_size -= l;
				total_len += l;
			}
			continue;
		} else if (is_s390x && sscanf(line, "processor %d:", &cpu) == 1) {
			char *p;

			if (use_view && max_cpus > 0 && (curcpu + 1) == max_cpus)
				break;

			if (!cpu_in_cpuset(cpu, cpuset))
				continue;

			curcpu ++;
			p = strchr(line, ':');
			if (!p || !*p)
				return 0;
			p++;

			l = snprintf(cache, cache_size, "processor %d:%s", curcpu, p);
			if (l < 0)
				return log_error(0, "Failed to write cache");
			if (l >= cache_size)
				return log_error(0, "Write to cache was truncated");

			cache += l;
			cache_size -= l;
			total_len += l;
			continue;

		}
		if (am_printing) {
			l = snprintf(cache, cache_size, "%s", line);
			if (l < 0)
				return log_error(0, "Failed to write cache");
			if (l >= cache_size)
				return log_error(0, "Write to cache was truncated");

			cache += l;
			cache_size -= l;
			total_len += l;
		}
	}

	if (is_s390x) {
		__do_free char *origcache = d->buf;
		ssize_t l;

		d->buf = malloc(d->buflen);
		if (!d->buf) {
			d->buf = move_ptr(origcache);
			return 0;
		}

		cache = d->buf;
		cache_size = d->buflen;
		total_len = 0;
		l = snprintf(cache, cache_size, "vendor_id       : IBM/S390\n");
		if (l < 0 || l >= cache_size)
			return 0;

		cache_size -= l;
		cache += l;
		total_len += l;
		l = snprintf(cache, cache_size, "# processors    : %d\n", curcpu + 1);
		if (l < 0 || l >= cache_size)
			return 0;

		cache_size -= l;
		cache += l;
		total_len += l;
		l = snprintf(cache, cache_size, "%s", origcache);
		if (l < 0 || l >= cache_size)
			return 0;
		total_len += l;
	}

	d->cached = 1;
	d->size = total_len;
	if (total_len > size)
		total_len = size;

	/* read from off 0 */
	memcpy(buf, d->buf, total_len);

	return total_len;
}

/*
 * Returns 0 on success.
 * It is the caller's responsibility to free `return_usage`, unless this
 * function returns an error.
 */
int read_cpuacct_usage_all(char *cg, char *cpuset,
			   struct cpuacct_usage **return_usage, int *size)
{
	__do_free char *usage_str = NULL;
	__do_free struct cpuacct_usage *cpu_usage = NULL;
	int i = 0, j = 0, read_pos = 0, read_cnt = 0;
	int cpucount;
	int ret;
	int cg_cpu;
	uint64_t cg_user, cg_system;
	int64_t ticks_per_sec;

	ticks_per_sec = sysconf(_SC_CLK_TCK);
	if (ticks_per_sec < 0 && errno == EINVAL) {
		lxcfs_debug("%m - Failed to determine number of ticks per second");
		return -1;
	}

	cpucount = get_nprocs_conf();
	cpu_usage = malloc(sizeof(struct cpuacct_usage) * cpucount);
	if (!cpu_usage)
		return -ENOMEM;

	memset(cpu_usage, 0, sizeof(struct cpuacct_usage) * cpucount);
	if (!cgroup_ops->get(cgroup_ops, "cpuacct", cg, "cpuacct.usage_all", &usage_str)) {
		char *sep = " \t\n";
		char *tok;

		/* Read cpuacct.usage_percpu instead. */
		lxcfs_debug("Falling back to cpuacct.usage_percpu");
		if (!cgroup_ops->get(cgroup_ops, "cpuacct", cg, "cpuacct.usage_percpu", &usage_str))
			return -1;

		lxc_iterate_parts(tok, usage_str, sep) {
			uint64_t percpu_user;

			if (i >= cpucount)
				break;

			tok = trim_whitespace_in_place(tok);
			ret = safe_uint64(tok, &percpu_user, 10);
			if (ret)
				return -1;

			/* Convert the time from nanoseconds to USER_HZ */
			cpu_usage[i].user = percpu_user / 1000.0 / 1000 / 1000 * ticks_per_sec;
			cpu_usage[i].system = cpu_usage[i].user;
			i++;
			lxcfs_debug("cpu%d with time %s", i, tok);
		}
	} else {
		if (sscanf(usage_str, "cpu user system\n%n", &read_cnt) != 0)
			return log_error(-1, "read_cpuacct_usage_all reading first line from %s/cpuacct.usage_all failed", cg);

		read_pos += read_cnt;

		for (i = 0, j = 0; i < cpucount; i++) {
			ret = sscanf(usage_str + read_pos,
					"%d %" PRIu64 " %" PRIu64 "\n%n", &cg_cpu,
					&cg_user, &cg_system, &read_cnt);

			if (ret == EOF)
				break;

			if (ret != 3)
				return log_error(-EINVAL, "Failed to parse cpuacct.usage_all line %s from cgroup %s",
						usage_str + read_pos, cg);

			read_pos += read_cnt;

			/* Convert the time from nanoseconds to USER_HZ */
			cpu_usage[j].user = cg_user / 1000.0 / 1000 / 1000 * ticks_per_sec;
			cpu_usage[j].system = cg_system / 1000.0 / 1000 / 1000 * ticks_per_sec;
			j++;
		}
	}

	*return_usage = move_ptr(cpu_usage);
	*size = cpucount;
	return 0;
}

static bool cpuview_init_head(struct cg_proc_stat_head **head)
{
	__do_free struct cg_proc_stat_head *h;

	h = zalloc(sizeof(struct cg_proc_stat_head));
	if (!h)
		return false;

	if (pthread_rwlock_init(&h->lock, NULL))
		return false;

	h->lastcheck = time(NULL);

	*head = move_ptr(h);
	return true;
}

bool init_cpuview(void)
{
	int i;

	for (i = 0; i < CPUVIEW_HASH_SIZE; i++)
		proc_stat_history[i] = NULL;

	for (i = 0; i < CPUVIEW_HASH_SIZE; i++) {
		if (!cpuview_init_head(&proc_stat_history[i]))
			goto err;
	}

	return true;

err:
	for (i = 0; i < CPUVIEW_HASH_SIZE; i++) {
		if (proc_stat_history[i])
			free_disarm(proc_stat_history[i]);
	}

	return false;
}

static void cpuview_free_head(struct cg_proc_stat_head *head)
{
	struct cg_proc_stat *node;

	if (head->next) {
		node = head->next;

		for (;;) {
			struct cg_proc_stat *cur = node;
			node = node->next;
			free_proc_stat_node(cur);
			if (!node)
				break;
		}
	}

	pthread_rwlock_destroy(&head->lock);
	free_disarm(head);
}

void free_cpuview(void)
{
	for (int i = 0; i < CPUVIEW_HASH_SIZE; i++)
		if (proc_stat_history[i])
			cpuview_free_head(proc_stat_history[i]);
}