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"
39 #include "pathnames.h"
41 /*#define CONFIG_ZRAM_DEBUG*/
43 #ifdef CONFIG_ZRAM_DEBUG
44 # define DBG(x) do { fputs("zram: ", stderr); x; fputc('\n', stderr); } while(0)
49 /* status output columns */
72 static const struct colinfo infos
[] = {
73 [COL_NAME
] = { "NAME", 0.25, 0, N_("zram device name") },
74 [COL_DISKSIZE
] = { "DISKSIZE", 5, SCOLS_FL_RIGHT
, N_("limit on the uncompressed amount of data") },
75 [COL_ORIG_SIZE
] = { "DATA", 5, SCOLS_FL_RIGHT
, N_("uncompressed size of stored data") },
76 [COL_COMP_SIZE
] = { "COMPR", 5, SCOLS_FL_RIGHT
, N_("compressed size of stored data") },
77 [COL_ALGORITHM
] = { "ALGORITHM", 3, 0, N_("the selected compression algorithm") },
78 [COL_STREAMS
] = { "STREAMS", 3, SCOLS_FL_RIGHT
, N_("number of concurrent compress operations") },
79 [COL_ZEROPAGES
] = { "ZERO-PAGES", 3, SCOLS_FL_RIGHT
, N_("empty pages with no allocated memory") },
80 [COL_MEMTOTAL
] = { "TOTAL", 5, SCOLS_FL_RIGHT
, N_("all memory including allocator fragmentation and metadata overhead") },
81 [COL_MEMLIMIT
] = { "MEM-LIMIT", 5, SCOLS_FL_RIGHT
, N_("memory limit used to store compressed data") },
82 [COL_MEMUSED
] = { "MEM-USED", 5, SCOLS_FL_RIGHT
, N_("memory zram have been consumed to store compressed data") },
83 [COL_MIGRATED
] = { "MIGRATED", 5, SCOLS_FL_RIGHT
, N_("number of objects migrated by compaction") },
84 [COL_MOUNTPOINT
]= { "MOUNTPOINT",0.10, SCOLS_FL_TRUNC
, N_("where the device is mounted") },
87 static int columns
[ARRAY_SIZE(infos
) * 2] = {-1};
91 MM_ORIG_DATA_SIZE
= 0,
100 static const char *mm_stat_names
[] = {
101 [MM_ORIG_DATA_SIZE
] = "orig_data_size",
102 [MM_COMPR_DATA_SIZE
] = "compr_data_size",
103 [MM_MEM_USED_TOTAL
] = "mem_used_total",
104 [MM_MEM_LIMIT
] = "mem_limit",
105 [MM_MEM_USED_MAX
] = "mem_used_max",
106 [MM_ZERO_PAGES
] = "zero_pages",
107 [MM_NUM_MIGRATED
] = "num_migrated"
112 struct sysfs_cxt sysfs
;
115 unsigned int mm_stat_probed
: 1,
117 has_control
: 1; /* has /sys/class/zram-control/ */
120 static unsigned int raw
, no_headings
, inbytes
;
122 static int get_column_id(int num
)
124 assert(num
< ncolumns
);
125 assert(columns
[num
] < (int) ARRAY_SIZE(infos
));
129 static const struct colinfo
*get_column_info(int num
)
131 return &infos
[ get_column_id(num
) ];
134 static int column_name_to_id(const char *name
, size_t namesz
)
138 for (i
= 0; i
< ARRAY_SIZE(infos
); i
++) {
139 const char *cn
= infos
[i
].name
;
141 if (!strncasecmp(name
, cn
, namesz
) && !*(cn
+ namesz
))
144 warnx(_("unknown column: %s"), name
);
148 static void zram_reset_stat(struct zram
*z
)
151 strv_free(z
->mm_stat
);
153 z
->mm_stat_probed
= 0;
157 static void zram_set_devname(struct zram
*z
, const char *devname
, size_t n
)
162 snprintf(z
->devname
, sizeof(z
->devname
), "/dev/zram%zu", n
);
164 strncpy(z
->devname
, devname
, sizeof(z
->devname
));
165 z
->devname
[sizeof(z
->devname
) - 1] = '\0';
168 DBG(fprintf(stderr
, "set devname: %s", z
->devname
));
169 sysfs_deinit(&z
->sysfs
);
173 static int zram_get_devnum(struct zram
*z
)
179 if (sscanf(z
->devname
, "/dev/zram%d", &n
) == 1)
184 static struct zram
*new_zram(const char *devname
)
186 struct zram
*z
= xcalloc(1, sizeof(struct zram
));
188 DBG(fprintf(stderr
, "new: %p", z
));
190 zram_set_devname(z
, devname
, 0);
194 static void free_zram(struct zram
*z
)
198 DBG(fprintf(stderr
, "free: %p", z
));
199 sysfs_deinit(&z
->sysfs
);
204 static struct sysfs_cxt
*zram_get_sysfs(struct zram
*z
)
208 if (!z
->sysfs
.devno
) {
209 dev_t devno
= sysfs_devname_to_devno(z
->devname
, NULL
);
212 if (sysfs_init(&z
->sysfs
, devno
, NULL
))
214 if (*z
->devname
!= '/') {
215 /* canonicalize the device name according to /sys */
217 if (sysfs_get_devname(&z
->sysfs
, name
, sizeof(name
)))
218 snprintf(z
->devname
, sizeof(z
->devname
), "/dev/%s", name
);
225 static inline int zram_exist(struct zram
*z
)
230 if (zram_get_sysfs(z
) == NULL
) {
235 DBG(fprintf(stderr
, "%s exists", z
->devname
));
239 static int zram_set_u64parm(struct zram
*z
, const char *attr
, uint64_t num
)
241 struct sysfs_cxt
*sysfs
= zram_get_sysfs(z
);
244 DBG(fprintf(stderr
, "%s writing %ju to %s", z
->devname
, num
, attr
));
245 return sysfs_write_u64(sysfs
, attr
, num
);
248 static int zram_set_strparm(struct zram
*z
, const char *attr
, const char *str
)
250 struct sysfs_cxt
*sysfs
= zram_get_sysfs(z
);
253 DBG(fprintf(stderr
, "%s writing %s to %s", z
->devname
, str
, attr
));
254 return sysfs_write_string(sysfs
, attr
, str
);
258 static int zram_used(struct zram
*z
)
261 struct sysfs_cxt
*sysfs
= zram_get_sysfs(z
);
264 sysfs_read_u64(sysfs
, "disksize", &size
) == 0 &&
267 DBG(fprintf(stderr
, "%s used", z
->devname
));
270 DBG(fprintf(stderr
, "%s unused", z
->devname
));
274 static int zram_has_control(struct zram
*z
)
276 if (!z
->control_probed
) {
277 z
->has_control
= access(_PATH_SYS_CLASS
"/zram-control/", F_OK
) == 0 ? 1 : 0;
278 z
->control_probed
= 1;
279 DBG(fprintf(stderr
, "zram-control: %s", z
->has_control
? "yes" : "no"));
282 return z
->has_control
;
285 static int zram_control_add(struct zram
*z
)
289 if (!zram_has_control(z
))
292 n
= path_read_s32(_PATH_SYS_CLASS
"/zram-control/hot_add");
296 DBG(fprintf(stderr
, "hot-add: %d", n
));
297 zram_set_devname(z
, NULL
, n
);
301 static int zram_control_remove(struct zram
*z
)
303 char str
[sizeof stringify_value(INT_MAX
)];
306 if (!zram_has_control(z
))
309 n
= zram_get_devnum(z
);
313 DBG(fprintf(stderr
, "hot-remove: %d", n
));
314 snprintf(str
, sizeof(str
), "%d", n
);
315 return path_write_str(str
, _PATH_SYS_CLASS
"/zram-control/hot_remove");
318 static struct zram
*find_free_zram(void)
320 struct zram
*z
= new_zram(NULL
);
324 for (i
= 0; isfree
== 0; i
++) {
325 DBG(fprintf(stderr
, "find free: checking zram%zu", i
));
326 zram_set_devname(z
, NULL
, i
);
327 if (!zram_exist(z
) && zram_control_add(z
) != 0)
329 isfree
= !zram_used(z
);
338 static char *get_mm_stat(struct zram
*z
, size_t idx
, int bytes
)
340 struct sysfs_cxt
*sysfs
;
344 assert(idx
< ARRAY_SIZE(mm_stat_names
));
347 sysfs
= zram_get_sysfs(z
);
351 /* Linux >= 4.1 uses /sys/block/zram<id>/mm_stat */
352 if (!z
->mm_stat
&& !z
->mm_stat_probed
) {
355 str
= sysfs_strdup(sysfs
, "mm_stat");
357 z
->mm_stat
= strv_split(str
, " ");
359 /* make sure kernel provides mm_stat as expected */
360 if (strv_length(z
->mm_stat
) < ARRAY_SIZE(mm_stat_names
)) {
361 strv_free(z
->mm_stat
);
365 z
->mm_stat_probed
= 1;
372 return xstrdup(z
->mm_stat
[idx
]);
374 num
= strtou64_or_err(z
->mm_stat
[idx
], _("Failed to parse mm_stat"));
375 return size_to_human_string(SIZE_SUFFIX_1LETTER
, num
);
378 /* Linux < 4.1 uses /sys/block/zram<id>/<attrname> */
379 name
= mm_stat_names
[idx
];
381 return sysfs_strdup(sysfs
, name
);
382 else if (sysfs_read_u64(sysfs
, name
, &num
) == 0)
383 return size_to_human_string(SIZE_SUFFIX_1LETTER
, num
);
387 static void fill_table_row(struct libscols_table
*tb
, struct zram
*z
)
389 static struct libscols_line
*ln
;
390 struct sysfs_cxt
*sysfs
;
397 DBG(fprintf(stderr
, "%s: filling status table", z
->devname
));
399 sysfs
= zram_get_sysfs(z
);
403 ln
= scols_table_new_line(tb
, NULL
);
405 err(EXIT_FAILURE
, _("failed to allocate output line"));
407 for (i
= 0; i
< (size_t) ncolumns
; i
++) {
410 switch (get_column_id(i
)) {
412 str
= xstrdup(z
->devname
);
416 str
= sysfs_strdup(sysfs
, "disksize");
417 else if (sysfs_read_u64(sysfs
, "disksize", &num
) == 0)
418 str
= size_to_human_string(SIZE_SUFFIX_1LETTER
, num
);
422 char *alg
= sysfs_strdup(sysfs
, "comp_algorithm");
425 if (strstr(alg
, "[lzo]") == NULL
) {
426 if (strstr(alg
, "[lz4]") == NULL
)
429 str
= xstrdup("lz4");
431 str
= xstrdup("lzo");
437 char path
[PATH_MAX
] = { '\0' };
440 check_mount_point(z
->devname
, &fl
, path
, sizeof(path
));
446 str
= sysfs_strdup(sysfs
, "max_comp_streams");
449 str
= get_mm_stat(z
, MM_ZERO_PAGES
, 1);
452 str
= get_mm_stat(z
, MM_ORIG_DATA_SIZE
, inbytes
);
455 str
= get_mm_stat(z
, MM_COMPR_DATA_SIZE
, inbytes
);
458 str
= get_mm_stat(z
, MM_MEM_USED_TOTAL
, inbytes
);
461 str
= get_mm_stat(z
, MM_MEM_LIMIT
, inbytes
);
464 str
= get_mm_stat(z
, MM_MEM_USED_MAX
, inbytes
);
467 str
= get_mm_stat(z
, MM_NUM_MIGRATED
, inbytes
);
470 if (str
&& scols_line_refer_data(ln
, i
, str
))
471 err(EXIT_FAILURE
, _("failed to add output data"));
475 static void status(struct zram
*z
)
477 struct libscols_table
*tb
;
482 tb
= scols_new_table();
484 err(EXIT_FAILURE
, _("failed to allocate output table"));
486 scols_table_enable_raw(tb
, raw
);
487 scols_table_enable_noheadings(tb
, no_headings
);
489 for (i
= 0; i
< (size_t) ncolumns
; i
++) {
490 const struct colinfo
*col
= get_column_info(i
);
492 if (!scols_table_new_column(tb
, col
->name
, col
->whint
, col
->flags
))
493 err(EXIT_FAILURE
, _("failed to initialize output column"));
497 fill_table_row(tb
, z
); /* just one device specified */
499 /* list all used devices */
503 zram_set_devname(z
, NULL
, i
);
507 fill_table_row(tb
, z
);
512 scols_print_table(tb
);
513 scols_unref_table(tb
);
516 static void __attribute__((__noreturn__
)) usage(void)
521 fputs(USAGE_HEADER
, out
);
522 fprintf(out
, _( " %1$s [options] <device>\n"
523 " %1$s -r <device> [...]\n"
524 " %1$s [options] -f | <device> -s <size>\n"),
525 program_invocation_short_name
);
527 fputs(USAGE_SEPARATOR
, out
);
528 fputs(_("Set up and control zram devices.\n"), out
);
530 fputs(USAGE_OPTIONS
, out
);
531 fputs(_(" -a, --algorithm lzo|lz4 compression algorithm to use\n"), out
);
532 fputs(_(" -b, --bytes print sizes in bytes rather than in human readable format\n"), out
);
533 fputs(_(" -f, --find find a free device\n"), out
);
534 fputs(_(" -n, --noheadings don't print headings\n"), out
);
535 fputs(_(" -o, --output <list> columns to use for status output\n"), out
);
536 fputs(_(" --raw use raw status output format\n"), out
);
537 fputs(_(" -r, --reset reset all specified devices\n"), out
);
538 fputs(_(" -s, --size <size> device size\n"), out
);
539 fputs(_(" -t, --streams <number> number of compression streams\n"), out
);
541 fputs(USAGE_SEPARATOR
, out
);
542 printf(USAGE_HELP_OPTIONS(27));
544 fputs(USAGE_COLUMNS
, out
);
545 for (i
= 0; i
< ARRAY_SIZE(infos
); i
++)
546 fprintf(out
, " %11s %s\n", infos
[i
].name
, _(infos
[i
].help
));
548 printf(USAGE_MAN_TAIL("zramctl(8)"));
561 int main(int argc
, char **argv
)
563 uintmax_t size
= 0, nstreams
= 0;
564 char *algorithm
= NULL
;
565 int rc
= 0, c
, find
= 0, act
= A_NONE
;
566 struct zram
*zram
= NULL
;
568 enum { OPT_RAW
= CHAR_MAX
+ 1 };
570 static const struct option longopts
[] = {
571 { "algorithm", required_argument
, NULL
, 'a' },
572 { "bytes", no_argument
, NULL
, 'b' },
573 { "find", no_argument
, NULL
, 'f' },
574 { "help", no_argument
, NULL
, 'h' },
575 { "output", required_argument
, NULL
, 'o' },
576 { "noheadings",no_argument
, NULL
, 'n' },
577 { "reset", no_argument
, NULL
, 'r' },
578 { "raw", no_argument
, NULL
, OPT_RAW
},
579 { "size", required_argument
, NULL
, 's' },
580 { "streams", required_argument
, NULL
, 't' },
581 { "version", no_argument
, NULL
, 'V' },
585 static const ul_excl_t excl
[] = {
590 int excl_st
[ARRAY_SIZE(excl
)] = UL_EXCL_STATUS_INIT
;
592 setlocale(LC_ALL
, "");
593 bindtextdomain(PACKAGE
, LOCALEDIR
);
595 atexit(close_stdout
);
597 while ((c
= getopt_long(argc
, argv
, "a:bfho:nrs:t:V", longopts
, NULL
)) != -1) {
599 err_exclusive_options(c
, longopts
, excl
, excl_st
);
603 if (strcmp(optarg
,"lzo") && strcmp(optarg
,"lz4"))
604 errx(EXIT_FAILURE
, _("unsupported algorithm: %s"),
615 ncolumns
= string_to_idarray(optarg
,
616 columns
, ARRAY_SIZE(columns
),
622 size
= strtosize_or_err(optarg
, _("failed to parse size"));
626 nstreams
= strtou64_or_err(optarg
, _("failed to parse streams"));
638 printf(UTIL_LINUX_VERSION
);
643 errtryhelp(EXIT_FAILURE
);
647 if (find
&& optind
< argc
)
648 errx(EXIT_FAILURE
, _("option --find is mutually exclusive "
651 act
= find
? A_FINDONLY
: A_STATUS
;
653 if (act
!= A_RESET
&& optind
+ 1 < argc
)
654 errx(EXIT_FAILURE
, _("only one <device> at a time is allowed"));
656 if ((act
== A_STATUS
|| act
== A_FINDONLY
) && (algorithm
|| nstreams
))
657 errx(EXIT_FAILURE
, _("options --algorithm and --streams "
658 "must be combined with --size"));
662 if (!ncolumns
) { /* default columns */
663 columns
[ncolumns
++] = COL_NAME
;
664 columns
[ncolumns
++] = COL_ALGORITHM
;
665 columns
[ncolumns
++] = COL_DISKSIZE
;
666 columns
[ncolumns
++] = COL_ORIG_SIZE
;
667 columns
[ncolumns
++] = COL_COMP_SIZE
;
668 columns
[ncolumns
++] = COL_MEMTOTAL
;
669 columns
[ncolumns
++] = COL_STREAMS
;
670 columns
[ncolumns
++] = COL_MOUNTPOINT
;
673 zram
= new_zram(argv
[optind
++]);
674 if (!zram_exist(zram
))
675 err(EXIT_FAILURE
, "%s", zram
->devname
);
682 errx(EXIT_FAILURE
, _("no device specified"));
683 while (optind
< argc
) {
684 zram
= new_zram(argv
[optind
]);
685 if (!zram_exist(zram
)
686 || zram_set_u64parm(zram
, "reset", 1)) {
687 warn(_("%s: failed to reset"), zram
->devname
);
690 zram_control_remove(zram
);
696 zram
= find_free_zram();
698 errx(EXIT_FAILURE
, _("no free zram device found"));
699 printf("%s\n", zram
->devname
);
704 zram
= find_free_zram();
706 errx(EXIT_FAILURE
, _("no free zram device found"));
707 } else if (optind
== argc
)
708 errx(EXIT_FAILURE
, _("no device specified"));
710 zram
= new_zram(argv
[optind
]);
711 if (!zram_exist(zram
))
712 err(EXIT_FAILURE
, "%s", zram
->devname
);
715 if (zram_set_u64parm(zram
, "reset", 1))
716 err(EXIT_FAILURE
, _("%s: failed to reset"), zram
->devname
);
719 zram_set_u64parm(zram
, "max_comp_streams", nstreams
))
720 err(EXIT_FAILURE
, _("%s: failed to set number of streams"), zram
->devname
);
723 zram_set_strparm(zram
, "comp_algorithm", algorithm
))
724 err(EXIT_FAILURE
, _("%s: failed to set algorithm"), zram
->devname
);
726 if (zram_set_u64parm(zram
, "disksize", size
))
727 err(EXIT_FAILURE
, _("%s: failed to set disksize (%ju bytes)"),
728 zram
->devname
, size
);
730 printf("%s\n", zram
->devname
);
735 return rc
? EXIT_FAILURE
: EXIT_SUCCESS
;