#!/bin/bash
# Author: Jan Ziak <0xe2.0x9a.0x9b@gmail.com>
set -e
export CCACHE_DIR=ccachedir
export CCACHE_MAXSIZE=500K
rm -rf "$CCACHE_DIR"
mkdir "$CCACHE_DIR"
ccache -s
LOG="$(basename $0).log"
rm -f "$LOG"
RANDOM=0
for((i=0; i<256; i++)); do
	n=$(($RANDOM % 8))
	m=$(($RANDOM % 8))
	echo $n $m >> "$LOG"
	echo "int f() { return $m; }" > $n.c
	CC=gcc
	#CC=clang
	ccache $CC -c -o $n.o $n.c
	rm $n.[co]
done
echo
echo "========"
echo
ccache -s
echo
md5sum "$LOG"
echo "$(cat "$LOG" | sort | uniq | wc -l) unique C files"
echo "$(find "$CCACHE_DIR" -name *.o -or -name *.manifest | wc -l) files in cache"
echo "$(find "$CCACHE_DIR" -type f -not -name stats -and -not -name *.TAG -and -not -name ccache.conf | wc -l) files in cache"
echo "$(find "$CCACHE_DIR" -name *.o -or -name *.manifest | xargs du -c --si | grep total) cache size at block granularity (1k = 1000 bytes)"
echo "$(find "$CCACHE_DIR" -name *.o -or -name *.manifest | xargs du -bc --si | grep total) cache size at byte granularity (1k = 1000 bytes)"
echo
echo "========"
echo
ccache -c
ccache -s
echo
echo "$(find "$CCACHE_DIR" -name *.o -or -name *.manifest | wc -l) files in cache"
echo "$(find "$CCACHE_DIR" -type f -not -name stats -and -not -name *.TAG -and -not -name ccache.conf | wc -l) files in cache"
echo "$(find "$CCACHE_DIR" -name *.o -or -name *.manifest | xargs du -c --si | grep total) cache size at block granularity (1k = 1000 bytes)"
echo "$(find "$CCACHE_DIR" -name *.o -or -name *.manifest | xargs du -bc --si | grep total) cache size at byte granularity (1k = 1000 bytes)"
