#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#define __STDC_FORMAT_MACROS
#include <inttypes.h>


void Usage(void){
	printf("Usage:\n");
	printf("\t./thp-limit [pages_total(4K)] [high_watermark(4K)] [low_watermark(4k)] [min_watermark(4K)]\n");
	printf("\tOutput:\n");
	printf("\tmin_free_kbytes\n");
	printf("\twatermark_scale_factor\n");
	printf("\twatermark_boost_factor\n");
}


int main(int argc, char **argv){
	uint64_t page_size = 4096;
	uint64_t pages_total;
	uint64_t high_watermark;
	uint64_t low_watermark;
	uint64_t min_watermark;
	uint64_t min_free_kbytes;
	uint64_t watermark_scale_factor;
	uint64_t watermark_boost_factor;

	if(argc != 5){
		Usage();
		return 0;
	}
	pages_total = (uint64_t)strtoull(argv[1], NULL, 0);
	high_watermark = (uint64_t)strtoull(argv[2], NULL, 0);
	low_watermark = (uint64_t)strtoull(argv[3], NULL, 0);
	min_watermark = (uint64_t)strtoull(argv[4], NULL, 0);

	//minimum number of kilobytes free
	//correlates to minwater mark
	min_free_kbytes = min_watermark*page_size/1024;
	//The unit is in fractions of 10,000. aggressiveness of kswapd, amount of memory left in a node/system before kswapd is woken up and how much memory needs to be free before kswapd goes back to sleep.
	//10 means the distances between watermarks are 0.1% of the available memory in the node/system.
	//correlates to low watermark
	watermark_scale_factor = low_watermark*10000/pages_total;
	//watermark_scale_factor = 10000*low_watermark/pages_total;
	//pages_total low_watermark*10000*pages_total = watermark_scale_factor
	//low_watermark = watermark_scale_factor/10000*pages_total

	//The unit is in fractions of 10,000. level of reclaim, percentage of the high watermark of a zone that will be reclaimed
	//correlates to high watermark
	watermark_boost_factor = watermark_scale_factor*high_watermark/low_watermark;

	printf("pages_total: %" PRIu64 "\n", pages_total);
	printf("high_watermark: %" PRIu64 "\n", high_watermark);
	printf("low_watermark: %" PRIu64 "\n", low_watermark);
	printf("min_watermark: %" PRIu64 "\n\n", min_watermark);
	printf("min_free_kbytes: %" PRIu64 "\n", min_free_kbytes);
	printf("watermark_scale_factor: %" PRIu64 "\n", watermark_scale_factor);
	printf("watermark_boost_factor: %" PRIu64 "\n", watermark_boost_factor);

	return 0;
}
