2 * zramctl - purpose of it
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"
38 /*#define CONFIG_ZRAM_DEBUG*/
40 #ifdef CONFIG_ZRAM_DEBUG
41 # define DBG(x) do { fputs("zram: ", stderr); x; fputc('\n', stderr); } while(0)
46 /* status output columns */
66 static const struct colinfo infos
[] = {
67 [COL_NAME
] = { "NAME", 0.25, 0, N_("zram device name") },
68 [COL_DISKSIZE
] = { "DISKSIZE", 5, SCOLS_FL_RIGHT
, N_("limit on the uncompressed worth of data") },
69 [COL_ORIG_SIZE
] = { "DATA", 5, SCOLS_FL_RIGHT
, N_("uncompressed size of stored data") },
70 [COL_COMP_SIZE
] = { "COMPR", 5, SCOLS_FL_RIGHT
, N_("compressed size of stored data") },
71 [COL_ALGORITHM
] = { "ALGORITHM", 3, 0, N_("selected compression algorithms") },
72 [COL_STREAMS
] = { "STREAMS", 3, SCOLS_FL_RIGHT
, N_("number of concurrent compress operations") },
73 [COL_ZEROPAGES
] = { "ZERO-PAGES", 3, SCOLS_FL_RIGHT
, N_("empty pages with no allocated memory") },
74 [COL_MEMTOTAL
] = { "TOTAL", 5, SCOLS_FL_RIGHT
, N_("all memory including allocator fragmentation and metadata overhead") },
75 [COL_MOUNTPOINT
]= { "MOUNTPOINT",0.10, SCOLS_FL_TRUNC
, N_("where the device is mounted") },
79 static int columns
[ARRAY_SIZE(infos
) * 2] = {-1};
84 struct sysfs_cxt sysfs
;
87 #define ZRAM_EMPTY { .devname = { '\0' }, .sysfs = UL_SYSFSCXT_EMPTY }
89 static unsigned int raw
, no_headings
, inbytes
;
92 static int get_column_id(int num
)
94 assert(num
< ncolumns
);
95 assert(columns
[num
] < (int) ARRAY_SIZE(infos
));
99 static const struct colinfo
*get_column_info(int num
)
101 return &infos
[ get_column_id(num
) ];
104 static int column_name_to_id(const char *name
, size_t namesz
)
108 for (i
= 0; i
< ARRAY_SIZE(infos
); i
++) {
109 const char *cn
= infos
[i
].name
;
111 if (!strncasecmp(name
, cn
, namesz
) && !*(cn
+ namesz
))
114 warnx(_("unknown column: %s"), name
);
118 static void zram_set_devname(struct zram
*z
, const char *devname
, size_t n
)
123 snprintf(z
->devname
, sizeof(z
->devname
), "/dev/zram%zu", n
);
125 strncpy(z
->devname
, devname
, sizeof(z
->devname
));
126 z
->devname
[sizeof(z
->devname
) - 1] = '\0';
129 DBG(fprintf(stderr
, "set devname: %s", z
->devname
));
130 sysfs_deinit(&z
->sysfs
);
133 static struct zram
*new_zram(const char *devname
)
135 struct zram
*z
= xcalloc(1, sizeof(struct zram
));
137 DBG(fprintf(stderr
, "new: %p", z
));
139 zram_set_devname(z
, devname
, 0);
143 static void free_zram(struct zram
*z
)
147 DBG(fprintf(stderr
, "free: %p", z
));
148 sysfs_deinit(&z
->sysfs
);
152 static struct sysfs_cxt
*zram_get_sysfs(struct zram
*z
)
156 if (!z
->sysfs
.devno
) {
157 dev_t devno
= sysfs_devname_to_devno(z
->devname
, NULL
);
160 if (sysfs_init(&z
->sysfs
, devno
, NULL
))
167 static inline int zram_exist(struct zram
*z
)
171 if (zram_get_sysfs(z
) == NULL
)
174 DBG(fprintf(stderr
, "%s exists", z
->devname
));
178 static int zram_set_u64parm(struct zram
*z
, const char *attr
, uint64_t num
)
180 struct sysfs_cxt
*sysfs
= zram_get_sysfs(z
);
183 DBG(fprintf(stderr
, "%s writing %ju to %s", z
->devname
, num
, attr
));
184 return sysfs_write_u64(sysfs
, attr
, num
);
187 static int zram_set_strparm(struct zram
*z
, const char *attr
, const char *str
)
189 struct sysfs_cxt
*sysfs
= zram_get_sysfs(z
);
192 DBG(fprintf(stderr
, "%s writing %s to %s", z
->devname
, str
, attr
));
193 return sysfs_write_string(sysfs
, attr
, str
);
197 static int zram_used(struct zram
*z
)
200 struct sysfs_cxt
*sysfs
= zram_get_sysfs(z
);
203 sysfs_read_u64(sysfs
, "disksize", &size
) == 0 &&
206 DBG(fprintf(stderr
, "%s used", z
->devname
));
209 DBG(fprintf(stderr
, "%s unused", z
->devname
));
213 static struct zram
*find_free_zram(void)
215 struct zram
*z
= new_zram(NULL
);
219 for (i
= 0; isfree
== 0; i
++) {
220 DBG(fprintf(stderr
, "find free: checking zram%zu", i
));
221 zram_set_devname(z
, NULL
, i
);
224 isfree
= !zram_used(z
);
233 static void fill_table_row(struct libscols_table
*tb
, struct zram
*z
)
235 static struct libscols_line
*ln
;
236 struct sysfs_cxt
*sysfs
;
243 DBG(fprintf(stderr
, "%s: filling status table", z
->devname
));
245 sysfs
= zram_get_sysfs(z
);
249 ln
= scols_table_new_line(tb
, NULL
);
251 err(EXIT_FAILURE
, _("failed to initialize output line"));
253 for (i
= 0; i
< (size_t) ncolumns
; i
++) {
256 switch (get_column_id(i
)) {
258 str
= xstrdup(z
->devname
);
262 str
= sysfs_strdup(sysfs
, "disksize");
263 else if (sysfs_read_u64(sysfs
, "disksize", &num
) == 0)
264 str
= size_to_human_string(SIZE_SUFFIX_1LETTER
, num
);
268 str
= sysfs_strdup(sysfs
, "orig_data_size");
269 else if (sysfs_read_u64(sysfs
, "orig_data_size", &num
) == 0)
270 str
= size_to_human_string(SIZE_SUFFIX_1LETTER
, num
);
274 str
= sysfs_strdup(sysfs
, "compr_data_size");
275 else if (sysfs_read_u64(sysfs
, "compr_data_size", &num
) == 0)
276 str
= size_to_human_string(SIZE_SUFFIX_1LETTER
, num
);
280 char *alg
= sysfs_strdup(sysfs
, "comp_algorithm");
283 if (strstr(alg
, "[lzo]") == NULL
) {
284 if (strstr(alg
, "[lz4]") == NULL
)
287 str
= xstrdup("lz4");
289 str
= xstrdup("lzo");
295 char path
[PATH_MAX
] = { '\0' };
298 check_mount_point(z
->devname
, &fl
, path
, sizeof(path
));
304 str
= sysfs_strdup(sysfs
, "max_comp_streams");
307 str
= sysfs_strdup(sysfs
, "zero_pages");
311 str
= sysfs_strdup(sysfs
, "mem_used_total");
312 else if (sysfs_read_u64(sysfs
, "mem_used_total", &num
) == 0)
313 str
= size_to_human_string(SIZE_SUFFIX_1LETTER
, num
);
318 scols_line_refer_data(ln
, i
, str
);
322 static void status(struct zram
*z
)
324 struct libscols_table
*tb
;
329 tb
= scols_new_table();
331 err(EXIT_FAILURE
, _("failed to initialize output table"));
333 scols_table_enable_raw(tb
, raw
);
334 scols_table_enable_noheadings(tb
, no_headings
);
336 for (i
= 0; i
< (size_t) ncolumns
; i
++) {
337 const struct colinfo
*col
= get_column_info(i
);
339 if (!scols_table_new_column(tb
, col
->name
, col
->whint
, col
->flags
))
340 err(EXIT_FAILURE
, _("failed to initialize output column"));
344 fill_table_row(tb
, z
); /* just one device specified */
346 size_t i
; /* list all used devices */
350 zram_set_devname(z
, NULL
, i
);
354 fill_table_row(tb
, z
);
359 scols_print_table(tb
);
360 scols_unref_table(tb
);
363 static void __attribute__ ((__noreturn__
)) usage(FILE * out
)
367 fputs(USAGE_HEADER
, out
);
368 fprintf(out
, _( " %1$s [options] <device>\n"
369 " %1$s -r <device> [...]\n"
370 " %1$s [options] -f | <device> -s <size>\n"),
371 program_invocation_short_name
);
373 fputs(USAGE_OPTIONS
, out
);
374 fputs(_(" -a, --algorithm <lzo|lz4> compression algorithm\n"), out
);
375 fputs(_(" -b, --bytes print sizes in bytes rather than in human readable format\n"), out
);
376 fputs(_(" -f, --find find free device\n"), out
);
377 fputs(_(" -n, --noheadings don't print headings\n"), out
);
378 fputs(_(" -o, --output <list> columns to use for status output\n"), out
);
379 fputs(_(" -r, --reset reset all specified devices\n"), out
);
380 fputs(_(" -s, --size <size> device size\n"), out
);
381 fputs(_(" --raw use raw status output format\n"), out
);
382 fputs(_(" -t, --streams <number> number of compressoin streams\n\n"), out
);
384 fputs(USAGE_SEPARATOR
, out
);
385 fputs(USAGE_HELP
, out
);
386 fputs(USAGE_VERSION
, out
);
388 fputs(_("\nAvailable columns (for --output):\n"), out
);
389 for (i
= 0; i
< ARRAY_SIZE(infos
); i
++)
390 fprintf(out
, " %11s %s\n", infos
[i
].name
, _(infos
[i
].help
));
392 fprintf(out
, USAGE_MAN_TAIL("zramctl(8)"));
393 exit(out
== stderr
? 1 : EXIT_SUCCESS
);
405 int main(int argc
, char **argv
)
407 uintmax_t size
= 0, nstreams
= 0;
408 char *algorithm
= NULL
;
409 int rc
= 0, c
, find
= 0, act
= A_NONE
;
410 struct zram
*zram
= NULL
;
412 enum { OPT_RAW
= CHAR_MAX
+ 1 };
414 static const struct option longopts
[] = {
415 { "algorithm", required_argument
, NULL
, 'a' },
416 { "find", no_argument
, NULL
, 'f' },
417 { "help", no_argument
, NULL
, 'h' },
418 { "bytes", no_argument
, NULL
, 'b' },
419 { "output", required_argument
, NULL
, 'o' },
420 { "noheadings",no_argument
, NULL
, 'n' },
421 { "reset", no_argument
, NULL
, 'r' },
422 { "raw", no_argument
, NULL
, OPT_RAW
},
423 { "size", required_argument
, NULL
, 's' },
424 { "streams", required_argument
, NULL
, 't' },
425 { "version", no_argument
, NULL
, 'V' },
429 static const ul_excl_t excl
[] = {
434 int excl_st
[ARRAY_SIZE(excl
)] = UL_EXCL_STATUS_INIT
;
436 setlocale(LC_ALL
, "");
437 bindtextdomain(PACKAGE
, LOCALEDIR
);
439 atexit(close_stdout
);
441 while ((c
= getopt_long(argc
, argv
, "a:bfho:nrs:t:V", longopts
, NULL
)) != -1) {
443 err_exclusive_options(c
, longopts
, excl
, excl_st
);
447 if (strcmp(optarg
,"lzo") && strcmp(optarg
,"lz4"))
448 errx(EXIT_FAILURE
, _("unsupported algorithm: %s"),
459 ncolumns
= string_to_idarray(optarg
,
460 columns
, ARRAY_SIZE(columns
),
466 size
= strtosize_or_err(optarg
, _("failed to parse size"));
470 nstreams
= strtou64_or_err(optarg
, _("failed to parse streams"));
482 printf(UTIL_LINUX_VERSION
);
491 if (find
&& optind
< argc
)
492 errx(EXIT_FAILURE
, _("option --find is mutually exclusive "
495 act
= find
? A_FINDONLY
: A_STATUS
;
497 if (act
!= A_RESET
&& optind
+ 1 < argc
)
498 usage(stderr
); /* only --reset holds more devices */
502 if (algorithm
|| find
|| nstreams
)
503 errx(EXIT_FAILURE
, _("options --algorithm, --find and "
504 "--streams are mutually exclusive."));
505 if (!ncolumns
) { /* default columns */
506 columns
[ncolumns
++] = COL_NAME
;
507 columns
[ncolumns
++] = COL_ALGORITHM
;
508 columns
[ncolumns
++] = COL_DISKSIZE
;
509 columns
[ncolumns
++] = COL_ORIG_SIZE
;
510 columns
[ncolumns
++] = COL_COMP_SIZE
;
511 columns
[ncolumns
++] = COL_MEMTOTAL
;
512 columns
[ncolumns
++] = COL_STREAMS
;
513 columns
[ncolumns
++] = COL_MOUNTPOINT
;
516 zram
= new_zram(argv
[optind
++]);
522 errx(EXIT_FAILURE
, _("no device specified"));
523 while (optind
< argc
) {
524 zram
= new_zram(argv
[optind
]);
525 if (zram_set_u64parm(zram
, "reset", 1)) {
526 warn(_("%s: failed to reset"), zram
->devname
);
534 zram
= find_free_zram();
536 errx(EXIT_FAILURE
, _("no found free zram device"));
537 printf("%s\n", zram
->devname
);
542 zram
= find_free_zram();
544 errx(EXIT_FAILURE
, _("no found free zram device"));
545 } else if (optind
== argc
)
546 errx(EXIT_FAILURE
, _("no device specified"));
548 zram
= new_zram(argv
[optind
]);
550 if (zram_set_u64parm(zram
, "reset", 1))
551 err(EXIT_FAILURE
, _("%s: failed to reset"), zram
->devname
);
554 zram_set_u64parm(zram
, "max_comp_streams", nstreams
))
555 err(EXIT_FAILURE
, _("%s: failed to set number of streams"), zram
->devname
);
558 zram_set_strparm(zram
, "comp_algorithm", algorithm
))
559 err(EXIT_FAILURE
, _("%s: failed to set algorithm"), zram
->devname
);
561 if (zram_set_u64parm(zram
, "disksize", size
))
562 err(EXIT_FAILURE
, _("%s: failed to set disksize (%ju bytes)"),
563 zram
->devname
, size
);
565 printf("%s\n", zram
->devname
);
570 return rc
? EXIT_FAILURE
: EXIT_SUCCESS
;