2 * zramctl - control compressed block devices in RAM
4 * Copyright (c) 2014 Timofey Titovets <Nefelim4ag@gmail.com>
5 * Copyright (C) 2014 Karel Zak <kzak@redhat.com>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it would be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include <libsmartcols.h>
31 #include "closestream.h"
36 #include "ismounted.h"
40 /*#define CONFIG_ZRAM_DEBUG*/
42 #ifdef CONFIG_ZRAM_DEBUG
43 # define DBG(x) do { fputs("zram: ", stderr); x; fputc('\n', stderr); } while(0)
48 /* status output columns */
71 static const struct colinfo infos
[] = {
72 [COL_NAME
] = { "NAME", 0.25, 0, N_("zram device name") },
73 [COL_DISKSIZE
] = { "DISKSIZE", 5, SCOLS_FL_RIGHT
, N_("limit on the uncompressed amount of data") },
74 [COL_ORIG_SIZE
] = { "DATA", 5, SCOLS_FL_RIGHT
, N_("uncompressed size of stored data") },
75 [COL_COMP_SIZE
] = { "COMPR", 5, SCOLS_FL_RIGHT
, N_("compressed size of stored data") },
76 [COL_ALGORITHM
] = { "ALGORITHM", 3, 0, N_("the selected compression algorithm") },
77 [COL_STREAMS
] = { "STREAMS", 3, SCOLS_FL_RIGHT
, N_("number of concurrent compress operations") },
78 [COL_ZEROPAGES
] = { "ZERO-PAGES", 3, SCOLS_FL_RIGHT
, N_("empty pages with no allocated memory") },
79 [COL_MEMTOTAL
] = { "TOTAL", 5, SCOLS_FL_RIGHT
, N_("all memory including allocator fragmentation and metadata overhead") },
80 [COL_MEMLIMIT
] = { "MEM-LIMIT", 5, SCOLS_FL_RIGHT
, N_("memory limit used to store compressed data") },
81 [COL_MEMUSED
] = { "MEM-USED", 5, SCOLS_FL_RIGHT
, N_("memory zram have been consumed to store compressed data") },
82 [COL_MIGRATED
] = { "MIGRATED", 5, SCOLS_FL_RIGHT
, N_("number of objects migrated by compaction") },
83 [COL_MOUNTPOINT
]= { "MOUNTPOINT",0.10, SCOLS_FL_TRUNC
, N_("where the device is mounted") },
86 static int columns
[ARRAY_SIZE(infos
) * 2] = {-1};
90 MM_ORIG_DATA_SIZE
= 0,
99 static const char *mm_stat_names
[] = {
100 [MM_ORIG_DATA_SIZE
] = "orig_data_size",
101 [MM_COMPR_DATA_SIZE
] = "compr_data_size",
102 [MM_MEM_USED_TOTAL
] = "mem_used_total",
103 [MM_MEM_LIMIT
] = "mem_limit",
104 [MM_MEM_USED_MAX
] = "mem_used_max",
105 [MM_ZERO_PAGES
] = "zero_pages",
106 [MM_NUM_MIGRATED
] = "num_migrated"
112 struct sysfs_cxt sysfs
;
115 unsigned int mm_stat_probed
: 1;
118 #define ZRAM_EMPTY { .devname = { '\0' }, .sysfs = UL_SYSFSCXT_EMPTY }
120 static unsigned int raw
, no_headings
, inbytes
;
123 static int get_column_id(int num
)
125 assert(num
< ncolumns
);
126 assert(columns
[num
] < (int) ARRAY_SIZE(infos
));
130 static const struct colinfo
*get_column_info(int num
)
132 return &infos
[ get_column_id(num
) ];
135 static int column_name_to_id(const char *name
, size_t namesz
)
139 for (i
= 0; i
< ARRAY_SIZE(infos
); i
++) {
140 const char *cn
= infos
[i
].name
;
142 if (!strncasecmp(name
, cn
, namesz
) && !*(cn
+ namesz
))
145 warnx(_("unknown column: %s"), name
);
149 static void zram_reset_stat(struct zram
*z
)
152 strv_free(z
->mm_stat
);
154 z
->mm_stat_probed
= 0;
158 static void zram_set_devname(struct zram
*z
, const char *devname
, size_t n
)
163 snprintf(z
->devname
, sizeof(z
->devname
), "/dev/zram%zu", n
);
165 strncpy(z
->devname
, devname
, sizeof(z
->devname
));
166 z
->devname
[sizeof(z
->devname
) - 1] = '\0';
169 DBG(fprintf(stderr
, "set devname: %s", z
->devname
));
170 sysfs_deinit(&z
->sysfs
);
174 static struct zram
*new_zram(const char *devname
)
176 struct zram
*z
= xcalloc(1, sizeof(struct zram
));
178 DBG(fprintf(stderr
, "new: %p", z
));
180 zram_set_devname(z
, devname
, 0);
184 static void free_zram(struct zram
*z
)
188 DBG(fprintf(stderr
, "free: %p", z
));
189 sysfs_deinit(&z
->sysfs
);
194 static struct sysfs_cxt
*zram_get_sysfs(struct zram
*z
)
198 if (!z
->sysfs
.devno
) {
199 dev_t devno
= sysfs_devname_to_devno(z
->devname
, NULL
);
202 if (sysfs_init(&z
->sysfs
, devno
, NULL
))
204 if (*z
->devname
!= '/') {
205 /* cannonicalize the device name according to /sys */
207 if (sysfs_get_devname(&z
->sysfs
, name
, sizeof(name
)))
208 snprintf(z
->devname
, sizeof(z
->devname
), "/dev/%s", name
);
215 static inline int zram_exist(struct zram
*z
)
220 if (zram_get_sysfs(z
) == NULL
) {
225 DBG(fprintf(stderr
, "%s exists", z
->devname
));
229 static int zram_set_u64parm(struct zram
*z
, const char *attr
, uint64_t num
)
231 struct sysfs_cxt
*sysfs
= zram_get_sysfs(z
);
234 DBG(fprintf(stderr
, "%s writing %ju to %s", z
->devname
, num
, attr
));
235 return sysfs_write_u64(sysfs
, attr
, num
);
238 static int zram_set_strparm(struct zram
*z
, const char *attr
, const char *str
)
240 struct sysfs_cxt
*sysfs
= zram_get_sysfs(z
);
243 DBG(fprintf(stderr
, "%s writing %s to %s", z
->devname
, str
, attr
));
244 return sysfs_write_string(sysfs
, attr
, str
);
248 static int zram_used(struct zram
*z
)
251 struct sysfs_cxt
*sysfs
= zram_get_sysfs(z
);
254 sysfs_read_u64(sysfs
, "disksize", &size
) == 0 &&
257 DBG(fprintf(stderr
, "%s used", z
->devname
));
260 DBG(fprintf(stderr
, "%s unused", z
->devname
));
264 static struct zram
*find_free_zram(void)
266 struct zram
*z
= new_zram(NULL
);
270 for (i
= 0; isfree
== 0; i
++) {
271 DBG(fprintf(stderr
, "find free: checking zram%zu", i
));
272 zram_set_devname(z
, NULL
, i
);
275 isfree
= !zram_used(z
);
285 static char *get_mm_stat(struct zram
*z
, size_t idx
, int bytes
)
287 struct sysfs_cxt
*sysfs
;
291 assert(idx
< ARRAY_SIZE(mm_stat_names
));
294 sysfs
= zram_get_sysfs(z
);
298 /* Linux >= 4.1 uses /sys/block/zram<id>/mm_stat */
299 if (!z
->mm_stat
&& !z
->mm_stat_probed
) {
302 str
= sysfs_strdup(sysfs
, "mm_stat");
304 z
->mm_stat
= strv_split(str
, " ");
305 if (strv_length(z
->mm_stat
) < ARRAY_SIZE(mm_stat_names
))
306 errx(EXIT_FAILURE
, _("Failed to parse mm_stat"));
308 z
->mm_stat_probed
= 1;
315 return xstrdup(z
->mm_stat
[idx
]);
317 num
= strtou64_or_err(z
->mm_stat
[idx
], _("Failed to parse mm_stat"));
318 return size_to_human_string(SIZE_SUFFIX_1LETTER
, num
);
321 /* Linux < 4.1 uses /sys/block/zram<id>/<attrname> */
322 name
= mm_stat_names
[idx
];
324 return sysfs_strdup(sysfs
, name
);
325 else if (sysfs_read_u64(sysfs
, name
, &num
) == 0)
326 return size_to_human_string(SIZE_SUFFIX_1LETTER
, num
);
330 static void fill_table_row(struct libscols_table
*tb
, struct zram
*z
)
332 static struct libscols_line
*ln
;
333 struct sysfs_cxt
*sysfs
;
340 DBG(fprintf(stderr
, "%s: filling status table", z
->devname
));
342 sysfs
= zram_get_sysfs(z
);
346 ln
= scols_table_new_line(tb
, NULL
);
348 err(EXIT_FAILURE
, _("failed to initialize output line"));
350 for (i
= 0; i
< (size_t) ncolumns
; i
++) {
353 switch (get_column_id(i
)) {
355 str
= xstrdup(z
->devname
);
359 str
= sysfs_strdup(sysfs
, "disksize");
360 else if (sysfs_read_u64(sysfs
, "disksize", &num
) == 0)
361 str
= size_to_human_string(SIZE_SUFFIX_1LETTER
, num
);
365 char *alg
= sysfs_strdup(sysfs
, "comp_algorithm");
368 if (strstr(alg
, "[lzo]") == NULL
) {
369 if (strstr(alg
, "[lz4]") == NULL
)
372 str
= xstrdup("lz4");
374 str
= xstrdup("lzo");
380 char path
[PATH_MAX
] = { '\0' };
383 check_mount_point(z
->devname
, &fl
, path
, sizeof(path
));
389 str
= sysfs_strdup(sysfs
, "max_comp_streams");
392 str
= get_mm_stat(z
, MM_ZERO_PAGES
, 1);
395 str
= get_mm_stat(z
, MM_ORIG_DATA_SIZE
, inbytes
);
398 str
= get_mm_stat(z
, MM_COMPR_DATA_SIZE
, inbytes
);
401 str
= get_mm_stat(z
, MM_MEM_USED_TOTAL
, inbytes
);
404 str
= get_mm_stat(z
, MM_MEM_LIMIT
, inbytes
);
407 str
= get_mm_stat(z
, MM_MEM_USED_MAX
, inbytes
);
410 str
= get_mm_stat(z
, MM_NUM_MIGRATED
, inbytes
);
414 scols_line_refer_data(ln
, i
, str
);
418 static void status(struct zram
*z
)
420 struct libscols_table
*tb
;
425 tb
= scols_new_table();
427 err(EXIT_FAILURE
, _("failed to initialize output table"));
429 scols_table_enable_raw(tb
, raw
);
430 scols_table_enable_noheadings(tb
, no_headings
);
432 for (i
= 0; i
< (size_t) ncolumns
; i
++) {
433 const struct colinfo
*col
= get_column_info(i
);
435 if (!scols_table_new_column(tb
, col
->name
, col
->whint
, col
->flags
))
436 err(EXIT_FAILURE
, _("failed to initialize output column"));
440 fill_table_row(tb
, z
); /* just one device specified */
442 /* list all used devices */
446 zram_set_devname(z
, NULL
, i
);
450 fill_table_row(tb
, z
);
455 scols_print_table(tb
);
456 scols_unref_table(tb
);
459 static void __attribute__ ((__noreturn__
)) usage(FILE * out
)
463 fputs(USAGE_HEADER
, out
);
464 fprintf(out
, _( " %1$s [options] <device>\n"
465 " %1$s -r <device> [...]\n"
466 " %1$s [options] -f | <device> -s <size>\n"),
467 program_invocation_short_name
);
469 fputs(USAGE_SEPARATOR
, out
);
470 fputs(_("Set up and control zram devices.\n"), out
);
472 fputs(USAGE_OPTIONS
, out
);
473 fputs(_(" -a, --algorithm lzo|lz4 compression algorithm to use\n"), out
);
474 fputs(_(" -b, --bytes print sizes in bytes rather than in human readable format\n"), out
);
475 fputs(_(" -f, --find find a free device\n"), out
);
476 fputs(_(" -n, --noheadings don't print headings\n"), out
);
477 fputs(_(" -o, --output <list> columns to use for status output\n"), out
);
478 fputs(_(" --raw use raw status output format\n"), out
);
479 fputs(_(" -r, --reset reset all specified devices\n"), out
);
480 fputs(_(" -s, --size <size> device size\n"), out
);
481 fputs(_(" -t, --streams <number> number of compression streams\n"), out
);
483 fputs(USAGE_SEPARATOR
, out
);
484 fputs(USAGE_HELP
, out
);
485 fputs(USAGE_VERSION
, out
);
487 fputs(_("\nAvailable columns (for --output):\n"), out
);
488 for (i
= 0; i
< ARRAY_SIZE(infos
); i
++)
489 fprintf(out
, " %11s %s\n", infos
[i
].name
, _(infos
[i
].help
));
491 fprintf(out
, USAGE_MAN_TAIL("zramctl(8)"));
492 exit(out
== stderr
? 1 : EXIT_SUCCESS
);
504 int main(int argc
, char **argv
)
506 uintmax_t size
= 0, nstreams
= 0;
507 char *algorithm
= NULL
;
508 int rc
= 0, c
, find
= 0, act
= A_NONE
;
509 struct zram
*zram
= NULL
;
511 enum { OPT_RAW
= CHAR_MAX
+ 1 };
513 static const struct option longopts
[] = {
514 { "algorithm", required_argument
, NULL
, 'a' },
515 { "bytes", no_argument
, NULL
, 'b' },
516 { "find", no_argument
, NULL
, 'f' },
517 { "help", no_argument
, NULL
, 'h' },
518 { "output", required_argument
, NULL
, 'o' },
519 { "noheadings",no_argument
, NULL
, 'n' },
520 { "reset", no_argument
, NULL
, 'r' },
521 { "raw", no_argument
, NULL
, OPT_RAW
},
522 { "size", required_argument
, NULL
, 's' },
523 { "streams", required_argument
, NULL
, 't' },
524 { "version", no_argument
, NULL
, 'V' },
528 static const ul_excl_t excl
[] = {
533 int excl_st
[ARRAY_SIZE(excl
)] = UL_EXCL_STATUS_INIT
;
535 setlocale(LC_ALL
, "");
536 bindtextdomain(PACKAGE
, LOCALEDIR
);
538 atexit(close_stdout
);
540 while ((c
= getopt_long(argc
, argv
, "a:bfho:nrs:t:V", longopts
, NULL
)) != -1) {
542 err_exclusive_options(c
, longopts
, excl
, excl_st
);
546 if (strcmp(optarg
,"lzo") && strcmp(optarg
,"lz4"))
547 errx(EXIT_FAILURE
, _("unsupported algorithm: %s"),
558 ncolumns
= string_to_idarray(optarg
,
559 columns
, ARRAY_SIZE(columns
),
565 size
= strtosize_or_err(optarg
, _("failed to parse size"));
569 nstreams
= strtou64_or_err(optarg
, _("failed to parse streams"));
581 printf(UTIL_LINUX_VERSION
);
590 if (find
&& optind
< argc
)
591 errx(EXIT_FAILURE
, _("option --find is mutually exclusive "
594 act
= find
? A_FINDONLY
: A_STATUS
;
596 if (act
!= A_RESET
&& optind
+ 1 < argc
)
597 errx(EXIT_FAILURE
, _("only one <device> at a time is allowed"));
599 if ((act
== A_STATUS
|| act
== A_FINDONLY
) && (algorithm
|| nstreams
))
600 errx(EXIT_FAILURE
, _("options --algorithm and --streams "
601 "must be combined with --size"));
605 if (!ncolumns
) { /* default columns */
606 columns
[ncolumns
++] = COL_NAME
;
607 columns
[ncolumns
++] = COL_ALGORITHM
;
608 columns
[ncolumns
++] = COL_DISKSIZE
;
609 columns
[ncolumns
++] = COL_ORIG_SIZE
;
610 columns
[ncolumns
++] = COL_COMP_SIZE
;
611 columns
[ncolumns
++] = COL_MEMTOTAL
;
612 columns
[ncolumns
++] = COL_STREAMS
;
613 columns
[ncolumns
++] = COL_MOUNTPOINT
;
616 zram
= new_zram(argv
[optind
++]);
617 if (!zram_exist(zram
))
618 err(EXIT_FAILURE
, "%s", zram
->devname
);
625 errx(EXIT_FAILURE
, _("no device specified"));
626 while (optind
< argc
) {
627 zram
= new_zram(argv
[optind
]);
628 if (!zram_exist(zram
)
629 || zram_set_u64parm(zram
, "reset", 1)) {
630 warn(_("%s: failed to reset"), zram
->devname
);
638 zram
= find_free_zram();
640 errx(EXIT_FAILURE
, _("no free zram device found"));
641 printf("%s\n", zram
->devname
);
646 zram
= find_free_zram();
648 errx(EXIT_FAILURE
, _("no free zram device found"));
649 } else if (optind
== argc
)
650 errx(EXIT_FAILURE
, _("no device specified"));
652 zram
= new_zram(argv
[optind
]);
653 if (!zram_exist(zram
))
654 err(EXIT_FAILURE
, "%s", zram
->devname
);
657 if (zram_set_u64parm(zram
, "reset", 1))
658 err(EXIT_FAILURE
, _("%s: failed to reset"), zram
->devname
);
661 zram_set_u64parm(zram
, "max_comp_streams", nstreams
))
662 err(EXIT_FAILURE
, _("%s: failed to set number of streams"), zram
->devname
);
665 zram_set_strparm(zram
, "comp_algorithm", algorithm
))
666 err(EXIT_FAILURE
, _("%s: failed to set algorithm"), zram
->devname
);
668 if (zram_set_u64parm(zram
, "disksize", size
))
669 err(EXIT_FAILURE
, _("%s: failed to set disksize (%ju bytes)"),
670 zram
->devname
, size
);
672 printf("%s\n", zram
->devname
);
677 return rc
? EXIT_FAILURE
: EXIT_SUCCESS
;