Skip to content

Instantly share code, notes, and snippets.

@mrpre
Created February 5, 2026 04:13
Show Gist options
  • Select an option

  • Save mrpre/00432c6154250326994fbeaf62e0e6f1 to your computer and use it in GitHub Desktop.

Select an option

Save mrpre/00432c6154250326994fbeaf62e0e6f1 to your computer and use it in GitHub Desktop.
/*
* Test program for zswpraw per-cgroup stat.
* Pre-compress data with zstd so kernel zswap can't compress further.
* Dep: apt-get install libzstd-dev
* Build: gcc -o test_zswpraw test_zswpraw.c -lzstd
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <zstd.h>
#define MB (1024 * 1024)
#define SIZE (100 * MB)
int main(void)
{
char *src, *dst;
size_t dst_size, compressed_size;
/* Allocate source buffer and fill with random-ish data */
src = malloc(SIZE);
if (!src) {
perror("malloc src");
return 1;
}
for (size_t i = 0; i < SIZE; i++)
src[i] = rand();
/* Compress with zstd */
dst_size = ZSTD_compressBound(SIZE);
dst = malloc(dst_size);
if (!dst) {
perror("malloc dst");
return 1;
}
compressed_size = ZSTD_compress(dst, dst_size, src, SIZE, 3);
if (ZSTD_isError(compressed_size)) {
fprintf(stderr, "ZSTD error: %s\n", ZSTD_getErrorName(compressed_size));
return 1;
}
free(src);
printf("Compressed %d MB -> %zu MB (%.1f%%)\n",
SIZE / MB, compressed_size / MB,
100.0 * compressed_size / SIZE);
printf("PID: %d, sleeping... (trigger memory.reclaim now)\n", getpid());
/* Keep compressed data in memory, sleep for reclaim test */
sleep(300);
free(dst);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment