-
Notifications
You must be signed in to change notification settings - Fork 246
/
proc_fuse.c
1514 lines (1280 loc) · 44.2 KB
/
proc_fuse.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
/* SPDX-License-Identifier: LGPL-2.1+ */
#include "config.h"
#include <dirent.h>
#include <errno.h>
#include <fcntl.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 "proc_fuse.h"
#include "bindings.h"
#include "cgroup_fuse.h"
#include "cgroups/cgroup.h"
#include "cgroups/cgroup_utils.h"
#include "cpuset_parse.h"
#include "lxcfs_fuse_compat.h"
#include "memory_utils.h"
#include "proc_loadavg.h"
#include "proc_cpuview.h"
#include "utils.h"
struct memory_stat {
uint64_t hierarchical_memory_limit;
uint64_t hierarchical_memsw_limit;
uint64_t total_cache;
uint64_t total_rss;
uint64_t total_rss_huge;
uint64_t total_shmem;
uint64_t total_mapped_file;
uint64_t total_dirty;
uint64_t total_writeback;
uint64_t total_swap;
uint64_t total_pgpgin;
uint64_t total_pgpgout;
uint64_t total_pgfault;
uint64_t total_pgmajfault;
uint64_t total_inactive_anon;
uint64_t total_active_anon;
uint64_t total_inactive_file;
uint64_t total_active_file;
uint64_t total_unevictable;
};
static off_t get_procfile_size(const char *path)
{
__do_fclose FILE *f = NULL;
__do_free char *line = NULL;
size_t len = 0;
ssize_t sz, answer = 0;
f = fopen(path, "re");
if (!f)
return 0;
while ((sz = getline(&line, &len, f)) != -1)
answer += sz;
return answer;
}
__lxcfs_fuse_ops int proc_getattr(const char *path, struct stat *sb)
{
struct timespec now;
memset(sb, 0, sizeof(struct stat));
if (clock_gettime(CLOCK_REALTIME, &now) < 0)
return -EINVAL;
sb->st_uid = sb->st_gid = 0;
sb->st_atim = sb->st_mtim = sb->st_ctim = now;
if (strcmp(path, "/proc") == 0) {
sb->st_mode = S_IFDIR | 00555;
sb->st_nlink = 2;
return 0;
}
if (strcmp(path, "/proc/meminfo") == 0 ||
strcmp(path, "/proc/cpuinfo") == 0 ||
strcmp(path, "/proc/uptime") == 0 ||
strcmp(path, "/proc/stat") == 0 ||
strcmp(path, "/proc/diskstats") == 0 ||
strcmp(path, "/proc/swaps") == 0 ||
strcmp(path, "/proc/loadavg") == 0 ||
strcmp(path, "/proc/slabinfo") == 0) {
sb->st_size = get_procfile_size(path);
sb->st_mode = S_IFREG | 00444;
sb->st_nlink = 1;
return 0;
}
return -ENOENT;
}
__lxcfs_fuse_ops int proc_readdir(const char *path, void *buf,
fuse_fill_dir_t filler, off_t offset,
struct fuse_file_info *fi)
{
if (DIR_FILLER(filler, buf, ".", NULL, 0) != 0 ||
DIR_FILLER(filler, buf, "..", NULL, 0) != 0 ||
DIR_FILLER(filler, buf, "cpuinfo", NULL, 0) != 0 ||
DIR_FILLER(filler, buf, "meminfo", NULL, 0) != 0 ||
DIR_FILLER(filler, buf, "stat", NULL, 0) != 0 ||
DIR_FILLER(filler, buf, "uptime", NULL, 0) != 0 ||
DIR_FILLER(filler, buf, "diskstats", NULL, 0) != 0 ||
DIR_FILLER(filler, buf, "swaps", NULL, 0) != 0 ||
DIR_FILLER(filler, buf, "loadavg", NULL, 0) != 0 ||
DIR_FILLER(filler, buf, "slabinfo", NULL, 0) != 0)
return -EINVAL;
return 0;
}
__lxcfs_fuse_ops int proc_open(const char *path, struct fuse_file_info *fi)
{
__do_free struct file_info *info = NULL;
int type = -1;
if (strcmp(path, "/proc/meminfo") == 0)
type = LXC_TYPE_PROC_MEMINFO;
else if (strcmp(path, "/proc/cpuinfo") == 0)
type = LXC_TYPE_PROC_CPUINFO;
else if (strcmp(path, "/proc/uptime") == 0)
type = LXC_TYPE_PROC_UPTIME;
else if (strcmp(path, "/proc/stat") == 0)
type = LXC_TYPE_PROC_STAT;
else if (strcmp(path, "/proc/diskstats") == 0)
type = LXC_TYPE_PROC_DISKSTATS;
else if (strcmp(path, "/proc/swaps") == 0)
type = LXC_TYPE_PROC_SWAPS;
else if (strcmp(path, "/proc/loadavg") == 0)
type = LXC_TYPE_PROC_LOADAVG;
else if (strcmp(path, "/proc/slabinfo") == 0)
type = LXC_TYPE_PROC_SLABINFO;
if (type == -1)
return -ENOENT;
info = zalloc(sizeof(*info));
if (!info)
return -ENOMEM;
info->type = type;
info->buflen = get_procfile_size(path) + BUF_RESERVE_SIZE;
info->buf = zalloc(info->buflen);
if (!info->buf)
return -ENOMEM;
/* set actual size to buffer size */
info->size = info->buflen;
fi->fh = PTR_TO_UINT64(move_ptr(info));
return 0;
}
__lxcfs_fuse_ops int proc_access(const char *path, int mask)
{
if (strcmp(path, "/proc") == 0 && access(path, R_OK) == 0)
return 0;
/* these are all read-only */
if ((mask & ~R_OK) != 0)
return -EACCES;
return 0;
}
__lxcfs_fuse_ops int proc_release(const char *path, struct fuse_file_info *fi)
{
do_release_file_info(fi);
return 0;
}
static uint64_t get_memlimit(const char *cgroup, bool swap)
{
__do_free char *memlimit_str = NULL;
uint64_t memlimit = 0;
int ret;
if (swap)
ret = cgroup_ops->get_memory_swap_max(cgroup_ops, cgroup, &memlimit_str);
else
ret = cgroup_ops->get_memory_max(cgroup_ops, cgroup, &memlimit_str);
if (ret > 0 && memlimit_str[0] && safe_uint64(memlimit_str, &memlimit, 10) < 0)
lxcfs_error("Failed to convert memlimit %s", memlimit_str);
return memlimit;
}
/*
* This function taken from glibc-2.32, as POSIX dirname("/some-dir") will
* return "/some-dir" as opposed to "/", which breaks `get_min_memlimit()`
*/
static char *gnu_dirname(char *path)
{
static const char dot[] = ".";
char *last_slash;
/* Find last '/'. */
last_slash = path != NULL ? strrchr(path, '/') : NULL;
if (last_slash != NULL && last_slash != path && last_slash[1] == '\0') {
/* Determine whether all remaining characters are slashes. */
char *runp;
for (runp = last_slash; runp != path; --runp)
if (runp[-1] != '/')
break;
/* The '/' is the last character, we have to look further. */
if (runp != path)
last_slash = memrchr(path, '/', runp - path);
}
if (last_slash != NULL) {
/* Determine whether all remaining characters are slashes. */
char *runp;
for (runp = last_slash; runp != path; --runp)
if (runp[-1] != '/')
break;
/* Terminate the path. */
if (runp == path) {
/*
* The last slash is the first character in the string.
* We have to return "/". As a special case we have to
* return "//" if there are exactly two slashes at the
* beginning of the string. See XBD 4.10 Path Name
* Resolution for more information
*/
if (last_slash == path + 1)
++last_slash;
else
last_slash = path + 1;
} else
last_slash = runp;
last_slash[0] = '\0';
} else {
/*
* This assignment is ill-designed but the XPG specs require to
* return a string containing "." in any case no directory part
* is found and so a static and constant string is required.
*/
path = (char *)dot;
}
return path;
}
static uint64_t get_min_memlimit(const char *cgroup, bool swap)
{
__do_free char *copy = NULL;
uint64_t memlimit = 0, retlimit = 0;
copy = strdup(cgroup);
if (!copy)
return log_error_errno(0, ENOMEM, "Failed to allocate memory");
retlimit = get_memlimit(copy, swap);
/*
* If the cgroup doesn't start with / (probably won't happen), dirname()
* will terminate with "" instead of "/"
*/
while (*copy && strcmp(copy, "/") != 0) {
char *it = copy;
it = gnu_dirname(it);
memlimit = get_memlimit(it, swap);
if (memlimit > 0 && memlimit < retlimit)
retlimit = memlimit;
};
return retlimit;
}
static inline bool startswith(const char *line, const char *pref)
{
return strncmp(line, pref, strlen(pref)) == 0;
}
static void get_swap_info(const char *cgroup, uint64_t memlimit,
uint64_t memusage, uint64_t *swtotal,
uint64_t *swusage, uint64_t *memswpriority)
{
__do_free char *memswusage_str = NULL, *memswpriority_str = NULL;
uint64_t memswlimit = 0, memswusage = 0;
int ret;
*swtotal = *swusage = 0;
*memswpriority = 1;
memswlimit = get_min_memlimit(cgroup, true);
if (memswlimit > 0) {
ret = cgroup_ops->get_memory_swap_current(cgroup_ops, cgroup, &memswusage_str);
if (ret < 0 || safe_uint64(memswusage_str, &memswusage, 10) != 0)
return;
if (liblxcfs_memory_is_cgroupv2()) {
*swtotal = memswlimit / 1024;
*swusage = memswusage / 1024;
} else {
if (memlimit > memswlimit)
*swtotal = 0;
else
*swtotal = (memswlimit - memlimit) / 1024;
if (memusage > memswusage || swtotal == 0)
*swusage = 0;
else
*swusage = (memswusage - memusage) / 1024;
}
ret = cgroup_ops->get_memory_swappiness(cgroup_ops, cgroup, &memswpriority_str);
if (ret >= 0)
safe_uint64(memswpriority_str, memswpriority, 10);
}
}
static int proc_swaps_read(char *buf, size_t size, off_t offset,
struct fuse_file_info *fi)
{
__do_free char *cgroup = NULL, *memusage_str = NULL,
*memswusage_str = NULL, *memswpriority_str = NULL;
struct fuse_context *fc = fuse_get_context();
bool wants_swap = lxcfs_has_opt(fuse_get_context()->private_data, LXCFS_SWAP_ON);
struct file_info *d = INTTYPE_TO_PTR(fi->fh);
uint64_t memlimit = 0, memusage = 0,
swtotal = 0, swusage = 0, memswpriority = 1,
hostswtotal = 0, hostswfree = 0;
ssize_t total_len = 0;
ssize_t l = 0;
char *cache = d->buf;
int ret;
__do_free char *line = NULL;
__do_free void *fopen_cache = NULL;
__do_fclose FILE *f = NULL;
size_t linelen = 0;
if (offset) {
size_t 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;
cgroup = get_pid_cgroup(initpid, "memory");
if (!cgroup)
return read_file_fuse("/proc/swaps", buf, size, d);
prune_init_slice(cgroup);
memlimit = get_min_memlimit(cgroup, false);
ret = cgroup_ops->get_memory_current(cgroup_ops, cgroup, &memusage_str);
if (ret < 0)
return 0;
if (safe_uint64(memusage_str, &memusage, 10) < 0)
lxcfs_error("Failed to convert memusage %s", memusage_str);
if (wants_swap)
get_swap_info(cgroup, memlimit, memusage, &swtotal, &swusage, &memswpriority);
total_len = snprintf(d->buf, d->size, "Filename\t\t\t\tType\t\tSize\tUsed\tPriority\n");
/* Read host total and free values */
f = fopen_cached("/proc/meminfo", "re", &fopen_cache);
if (!f)
return 0;
while (getline(&line, &linelen, f) != -1) {
if (startswith(line, "SwapTotal:"))
sscanf(line, "SwapTotal: %8" PRIu64 " kB", &hostswtotal);
else if (startswith(line, "SwapFree:"))
sscanf(line, "SwapFree: %8" PRIu64 " kB", &hostswfree);
}
if (wants_swap) {
/* The total amount of swap is always reported to be the
lesser of the RAM+SWAP limit or the SWAP device size.
This is because the kernel can swap as much as it
wants and not only up to swtotal. */
swtotal = memlimit / 1024 + swtotal;
if (hostswtotal < swtotal) {
swtotal = hostswtotal;
}
/* When swappiness is 0, pretend we can't swap. */
if (memswpriority == 0) {
swtotal = swusage;
}
}
if (swtotal > 0) {
l = snprintf(d->buf + total_len, d->size - total_len,
"none%*svirtual\t\t%" PRIu64 "\t%" PRIu64 "\t0\n",
36, " ", swtotal, swusage);
total_len += l;
}
if (total_len < 0 || l < 0)
return log_error(0, "Failed writing to cache");
d->cached = 1;
d->size = (int)total_len;
if ((size_t)total_len > size)
total_len = size;
memcpy(buf, d->buf, total_len);
return total_len;
}
static void get_blkio_io_value(char *str, unsigned major, unsigned minor,
char *iotype, uint64_t *v)
{
char *eol;
char key[32];
size_t len;
memset(key, 0, 32);
snprintf(key, 32, "%u:%u %s", major, minor, iotype);
*v = 0;
len = strlen(key);
while (*str) {
if (startswith(str, key)) {
sscanf(str + len, "%" PRIu64, v);
return;
}
eol = strchr(str, '\n');
if (!eol)
return;
str = eol + 1;
}
}
struct lxcfs_diskstats {
unsigned int major; /* 1 - major number */
unsigned int minor; /* 2 - minor mumber */
char dev_name[72]; /* 3 - device name */
uint64_t read; /* 4 - reads completed successfully */
uint64_t read_merged; /* 5 - reads merged */
uint64_t read_sectors; /* 6 - sectors read */
uint64_t read_ticks; /* 7 - time spent reading (ms) */
uint64_t write; /* 8 - writes completed */
uint64_t write_merged; /* 9 - writes merged */
uint64_t write_sectors; /* 10 - sectors written */
uint64_t write_ticks; /* 11 - time spent writing (ms) */
uint64_t ios_pgr; /* 12 - I/Os currently in progress */
uint64_t total_ticks; /* 13 - time spent doing I/Os (ms) */
uint64_t rq_ticks; /* 14 - weighted time spent doing I/Os (ms) */
uint64_t discard; /* 15 - discards completed successfully (4.18+) */
uint64_t discard_merged; /* 16 - discards merged (4.18+) */
uint64_t discard_sectors; /* 17 - sectors discarded (4.18+) */
uint64_t discard_ticks; /* 18 - time spent discarding (4.18+) */
};
static int proc_diskstats_read(char *buf, size_t size, off_t offset,
struct fuse_file_info *fi)
{
__do_free char *cg = NULL, *io_serviced_str = NULL,
*io_merged_str = NULL, *io_service_bytes_str = NULL,
*io_wait_time_str = NULL, *io_service_time_str = NULL,
*line = NULL;
__do_free void *fopen_cache = NULL;
__do_fclose FILE *f = NULL;
struct fuse_context *fc = fuse_get_context();
struct file_info *d = INTTYPE_TO_PTR(fi->fh);
struct lxcfs_diskstats stats = {};
/* helper fields */
uint64_t read_service_time, write_service_time, discard_service_time, read_wait_time,
write_wait_time, discard_wait_time;
char *cache = d->buf;
size_t cache_size = d->buflen;
size_t linelen = 0, total_len = 0;
int i = 0;
int ret;
if (offset) {
size_t 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, "blkio");
if (!cg)
return read_file_fuse("/proc/diskstats", buf, size, d);
prune_init_slice(cg);
ret = cgroup_ops->get_io_serviced(cgroup_ops, cg, &io_serviced_str);
if (ret < 0) {
if (ret == -EOPNOTSUPP)
return read_file_fuse("/proc/diskstats", buf, size, d);
}
ret = cgroup_ops->get_io_merged(cgroup_ops, cg, &io_merged_str);
if (ret < 0) {
if (ret == -EOPNOTSUPP)
return read_file_fuse("/proc/diskstats", buf, size, d);
}
ret = cgroup_ops->get_io_service_bytes(cgroup_ops, cg, &io_service_bytes_str);
if (ret < 0) {
if (ret == -EOPNOTSUPP)
return read_file_fuse("/proc/diskstats", buf, size, d);
}
ret = cgroup_ops->get_io_wait_time(cgroup_ops, cg, &io_wait_time_str);
if (ret < 0) {
if (ret == -EOPNOTSUPP)
return read_file_fuse("/proc/diskstats", buf, size, d);
}
ret = cgroup_ops->get_io_service_time(cgroup_ops, cg, &io_service_time_str);
if (ret < 0) {
if (ret == -EOPNOTSUPP)
return read_file_fuse("/proc/diskstats", buf, size, d);
}
f = fopen_cached("/proc/diskstats", "re", &fopen_cache);
if (!f)
return 0;
while (getline(&line, &linelen, f) != -1) {
ssize_t l;
char lbuf[256];
i = sscanf(line, "%u %u %71s", &stats.major, &stats.minor, stats.dev_name);
if (i != 3)
continue;
get_blkio_io_value(io_serviced_str, stats.major, stats.minor, "Read", &stats.read);
get_blkio_io_value(io_serviced_str, stats.major, stats.minor, "Write", &stats.write);
get_blkio_io_value(io_serviced_str, stats.major, stats.minor, "Discard", &stats.discard);
get_blkio_io_value(io_merged_str, stats.major, stats.minor, "Read", &stats.read_merged);
get_blkio_io_value(io_merged_str, stats.major, stats.minor, "Write", &stats.write_merged);
get_blkio_io_value(io_merged_str, stats.major, stats.minor, "Discard", &stats.discard_merged);
get_blkio_io_value(io_service_bytes_str, stats.major, stats.minor, "Read", &stats.read_sectors);
stats.read_sectors = stats.read_sectors / 512;
get_blkio_io_value(io_service_bytes_str, stats.major, stats.minor, "Write", &stats.write_sectors);
stats.write_sectors = stats.write_sectors / 512;
get_blkio_io_value(io_service_bytes_str, stats.major, stats.minor, "Discard", &stats.discard_sectors);
stats.discard_sectors = stats.discard_sectors / 512;
get_blkio_io_value(io_service_time_str, stats.major, stats.minor, "Read", &read_service_time);
read_service_time = read_service_time / 1000000;
get_blkio_io_value(io_wait_time_str, stats.major, stats.minor, "Read", &read_wait_time);
read_wait_time = read_wait_time / 1000000;
stats.read_ticks = read_service_time + read_wait_time;
get_blkio_io_value(io_service_time_str, stats.major, stats.minor, "Write", &write_service_time);
write_service_time = write_service_time / 1000000;
get_blkio_io_value(io_wait_time_str, stats.major, stats.minor, "Write", &write_wait_time);
write_wait_time = write_wait_time / 1000000;
stats.write_ticks = write_service_time + write_wait_time;
get_blkio_io_value(io_service_time_str, stats.major, stats.minor, "Discard", &discard_service_time);
discard_service_time = discard_service_time / 1000000;
get_blkio_io_value(io_wait_time_str, stats.major, stats.minor, "Discard", &discard_wait_time);
discard_wait_time = discard_wait_time / 1000000;
stats.discard_ticks = discard_service_time + discard_wait_time;
get_blkio_io_value(io_service_time_str, stats.major, stats.minor, "Total", &stats.total_ticks);
stats.total_ticks = stats.total_ticks / 1000000;
memset(lbuf, 0, 256);
if (stats.read || stats.write || stats.read_merged || stats.write_merged ||
stats.read_sectors || stats.write_sectors || stats.read_ticks ||
stats.write_ticks || stats.ios_pgr || stats.total_ticks || stats.rq_ticks ||
stats.discard_merged || stats.discard_sectors || stats.discard_ticks)
snprintf(lbuf, 256, "%u %u %s %" PRIu64 "%" PRIu64 "%" PRIu64 "%" PRIu64 "%" PRIu64 "%" PRIu64 "%" PRIu64 "%" PRIu64 "%" PRIu64 "%" PRIu64 "%" PRIu64 "%" PRIu64 "%" PRIu64 "%" PRIu64 "\n",
stats.major,
stats.minor,
stats.dev_name,
stats.read,
stats.read_merged,
stats.read_sectors,
stats.read_ticks,
stats.write,
stats.write_merged,
stats.write_sectors,
stats.write_ticks,
stats.ios_pgr,
stats.total_ticks,
stats.rq_ticks,
stats.discard_merged,
stats.discard_sectors,
stats.discard_ticks);
else
continue;
l = snprintf(cache, cache_size, "%s", lbuf);
if (l < 0)
return log_error(0, "Failed to write cache");
if ((size_t)l >= cache_size)
return log_error(0, "Write to cache was truncated");
cache += l;
cache_size -= l;
total_len += l;
}
d->cached = 1;
d->size = total_len;
if (total_len > size)
total_len = size;
memcpy(buf, d->buf, total_len);
return total_len;
}
#ifdef RELOADTEST
static inline void iwashere(void)
{
mknod("/tmp/lxcfs-iwashere", S_IFREG, 0644);
}
#endif
/*
* This function retrieves the busy time of a group of tasks by looking at
* cpuacct.usage. Unfortunately, this only makes sense when the container has
* been given it's own cpuacct cgroup. If not, this function will take the busy
* time of all other taks that do not actually belong to the container into
* account as well. If someone has a clever solution for this please send a
* patch!
*/
static double get_reaper_busy(pid_t task)
{
__do_free char *cgroup = NULL, *usage_str = NULL;
uint64_t usage = 0;
pid_t initpid;
initpid = lookup_initpid_in_store(task);
if (initpid <= 0)
return 0;
cgroup = get_pid_cgroup(initpid, "cpuacct");
if (!cgroup)
return 0;
prune_init_slice(cgroup);
if (!cgroup_ops->get(cgroup_ops, "cpuacct", cgroup, "cpuacct.usage", &usage_str))
return 0;
if (safe_uint64(usage_str, &usage, 10) < 0)
lxcfs_error("Failed to convert usage %s", usage_str);
return ((double)usage / 1000000000);
}
static uint64_t get_reaper_start_time(pid_t pid)
{
__do_free void *fopen_cache = NULL;
__do_fclose FILE *f = NULL;
int ret;
uint64_t starttime;
char path[STRLITERALLEN("/proc/") + LXCFS_NUMSTRLEN64 +
STRLITERALLEN("/stat") + 1];
pid_t qpid;
qpid = lookup_initpid_in_store(pid);
if (qpid <= 0)
return ret_errno(EINVAL);
ret = snprintf(path, sizeof(path), "/proc/%d/stat", qpid);
if (ret < 0 || (size_t)ret >= sizeof(path))
return ret_errno(EINVAL);
f = fopen_cached(path, "re", &fopen_cache);
if (!f)
return ret_errno(EINVAL);
/* Note that the *scanf() argument supression requires that length
* modifiers such as "l" are omitted. Otherwise some compilers will yell
* at us. It's like telling someone you're not married and then asking
* if you can bring your wife to the party.
*/
ret = fscanf(f, "%*d " /* (1) pid %d */
"%*s " /* (2) comm %s */
"%*c " /* (3) state %c */
"%*d " /* (4) ppid %d */
"%*d " /* (5) pgrp %d */
"%*d " /* (6) session %d */
"%*d " /* (7) tty_nr %d */
"%*d " /* (8) tpgid %d */
"%*u " /* (9) flags %u */
"%*u " /* (10) minflt %lu */
"%*u " /* (11) cminflt %lu */
"%*u " /* (12) majflt %lu */
"%*u " /* (13) cmajflt %lu */
"%*u " /* (14) utime %lu */
"%*u " /* (15) stime %lu */
"%*d " /* (16) cutime %ld */
"%*d " /* (17) cstime %ld */
"%*d " /* (18) priority %ld */
"%*d " /* (19) nice %ld */
"%*d " /* (20) num_threads %ld */
"%*d " /* (21) itrealvalue %ld */
"%" PRIu64, /* (22) starttime %llu */
&starttime);
if (ret != 1)
return ret_errno(EINVAL);
return ret_set_errno(starttime, 0);
}
static double get_reaper_start_time_in_sec(pid_t pid)
{
uint64_t clockticks, ticks_per_sec;
int64_t ret;
double res = 0;
clockticks = get_reaper_start_time(pid);
if (clockticks <= 0)
return log_debug(0, "Failed to retrieve start time of pid %d", pid);
ret = sysconf(_SC_CLK_TCK);
if (ret < 0)
return log_debug(0, "Failed to determine number of clock ticks in a second");
ticks_per_sec = (uint64_t)ret;
res = (double)clockticks / ticks_per_sec;
return res;
}
static double get_reaper_age(pid_t pid)
{
uint64_t uptime_ms;
double procstart, procage;
/*
* We need to substract the time the process has started since system
* boot minus the time when the system has started to get the actual
* reaper age.
*/
procstart = get_reaper_start_time_in_sec(pid);
procage = procstart;
if (procstart > 0) {
int ret;
struct timespec spec;
ret = clock_gettime(CLOCK_BOOTTIME, &spec);
if (ret < 0)
return 0;
uptime_ms = (spec.tv_sec * 1000) + (spec.tv_nsec * 1e-6);
procage = (uptime_ms - (procstart * 1000)) / 1000;
}
return procage;
}
/*
* We read /proc/uptime and reuse its second field.
* For the first field, we use the mtime for the reaper for
* the calling pid as returned by getreaperage
*/
static int proc_uptime_read(char *buf, size_t size, off_t offset,
struct fuse_file_info *fi)
{
struct fuse_context *fc = fuse_get_context();
struct file_info *d = INTTYPE_TO_PTR(fi->fh);
char *cache = d->buf;
ssize_t total_len = 0, ret = 0;
double busytime, idletime, reaperage;
#ifdef RELOADTEST
iwashere();
#endif
if (offset) {
size_t 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;
}
reaperage = get_reaper_age(fc->pid);
/*
* To understand why this is done, please read the comment to the
* get_reaper_busy() function.
*/
idletime = reaperage;
busytime = get_reaper_busy(fc->pid);
if (reaperage >= busytime)
idletime = reaperage - busytime;
ret = snprintf(d->buf, d->buflen, "%.2lf %.2lf\n", reaperage, idletime);
if (ret < 0 || ret >= d->buflen)
return read_file_fuse("/proc/uptime", buf, size, d);
total_len = ret;
d->cached = 1;
d->size = total_len;
if ((size_t)total_len > size)
total_len = size;
memcpy(buf, d->buf, total_len);
return total_len;
}
#define CPUALL_MAX_SIZE (BUF_RESERVE_SIZE / 2)
static int proc_stat_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_free struct cpuacct_usage *cg_cpu_usage = 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;
int curcpu = -1; /* cpu numbering starts at 0 */
int physcpu = 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, nice_sum = 0, system_sum = 0, idle_sum = 0,
iowait_sum = 0, irq_sum = 0, softirq_sum = 0, steal_sum = 0,
guest_sum = 0, guest_nice_sum = 0;
char cpuall[CPUALL_MAX_SIZE];
/* reserve for cpu all */
char *cache = d->buf + CPUALL_MAX_SIZE;
size_t cache_size = d->buflen - CPUALL_MAX_SIZE;
int cg_cpu_usage_size = 0;
if (offset) {
size_t left;
if (offset > d->size)
return -EINVAL;
if (!d->cached)
return 0;
left = d->size - offset;
total_len = left > size ? size : left;
memcpy(buf, d->buf + 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;
/*
* when container run with host pid namespace initpid == 1, cgroup will "/"
* we should return host os's /proc contents.
* in some case cpuacct_usage.all in "/" will larger then /proc/stat
*/
if (initpid == 1)
return read_file_fuse("/proc/stat", buf, size, d);
cg = get_pid_cgroup(initpid, "cpuset");
if (!cg)
return read_file_fuse("/proc/stat", buf, size, d);
prune_init_slice(cg);
cpuset = get_cpuset(cg);
if (!cpuset)
return 0;
f = fopen_cached("/proc/stat", "re", &fopen_cache);
if (!f)
return 0;
/* Skip first system cpu line. */
if (getline(&line, &linelen, f) < 0)
return log_error(0, "proc_stat_read read first line failed");
/*
* Read cpuacct.usage_all for all CPUs.
* If the cpuacct cgroup is present, it is used to calculate the container's
* CPU usage. If not, values from the host's /proc/stat are used.
*/
if (read_cpuacct_usage_all(cg, cpuset, &cg_cpu_usage, &cg_cpu_usage_size) == 0) {
if (cgroup_ops->can_use_cpuview(cgroup_ops) && opts && opts->use_cfs) {
total_len = cpuview_proc_stat(cg, cpuset, cg_cpu_usage,
cg_cpu_usage_size, f,
d->buf, d->buflen);
goto out;
}
} else {
lxcfs_v("proc_stat_read failed to read from cpuacct, falling back to the host's /proc/stat");
}
while (getline(&line, &linelen, f) != -1) {
ssize_t l;
char cpu_char[10]; /* That's a lot of cores */
char *c;
uint64_t all_used, cg_used, new_idle;
int ret;
if (strlen(line) == 0)
continue;
if (sscanf(line, "cpu%9[^ ]", cpu_char) != 1) {
/* not a ^cpuN line containing a number N, just print it */
l = snprintf(cache, cache_size, "%s", line);
if (l < 0)
return log_error(0, "Failed to write cache");
if ((size_t)l >= cache_size)
return log_error(0, "Write to cache was truncated");
cache += l;
cache_size -= l;
total_len += l;
continue;
}
if (sscanf(cpu_char, "%d", &physcpu) != 1)
continue;
if (!cpu_in_cpuset(physcpu, cpuset))
continue;
curcpu++;
ret = sscanf(line, "%*s %" PRIu64 "%" PRIu64 "%" PRIu64 "%" PRIu64 "%" PRIu64 "%" PRIu64 "%" PRIu64 "%" PRIu64 "%" PRIu64 "%" PRIu64,
&user,
&nice,
&system,
&idle,
&iowait,
&irq,
&softirq,
&steal,
&guest,
&guest_nice);
if (ret != 10 || !cg_cpu_usage) {
c = strchr(line, ' ');
if (!c)
continue;
l = snprintf(cache, cache_size, "cpu%d%s", curcpu, c);
if (l < 0)
return log_error(0, "Failed to write cache");