<commit_before>#include <iostream>
#include "clock.h"

size_t CLOCK_MAX_VALUE = 15;

Clock::Clock(stats stat) : 
		policy(stat), 
		clockLru(), 
		allObjects(),
		lruSize(0),
		noZeros(0),
		firstEviction(false),
		clockIt()
{
}

Clock::~Clock() {}

size_t Clock::get_bytes_cached() const { return lruSize; }

size_t Clock::proc(const request* r, bool warmup) {
	if (!warmup) {stat.accesses++;}

	auto searchRKId = allObjects.find(r->kid);
	if (searchRKId != allObjects.end()) {
		Clock::ClockItem& item = searchRKId->second;
		ClockLru::iterator& clockItemIt = item.clockLruIt;

		if (r->size() == item.size ) { 
			if (!warmup) {stat.hits++;}
			clockItemIt->second = CLOCK_MAX_VALUE;
			return 1;
		} else {
			clockLru.erase(clockItemIt);
			lruSize -= item.size;
			allObjects.erase(item.kId);
		}	
	}
	Clock::ClockItem newItem;
	newItem.kId = r->kid;
	newItem.size = r->size();
	
	assert((size_t)newItem.size <= stat.global_mem);

	while (lruSize + newItem.size > stat.global_mem) {	
		if (!firstEviction) { firstEviction = true;}		
		bool isDeleted = false;
		ClockLru::iterator tmpIt = clockIt;
		
		while(clockIt != clockLru.end()) {
			assert(clockIt->second <= CLOCK_MAX_VALUE);
			if (clockIt->second == 0) {
				tmpIt++;
				deleteItem(clockIt->first);
				isDeleted = true;
				break;
			} else {
				assert(clockIt->second > 0);
				clockIt->second--;
			}
			clockIt++;
		}
		if (!isDeleted) {
			clockIt = clockLru.begin();
			assert(clockIt->second <= CLOCK_MAX_VALUE);
			while (clockIt != tmpIt) {
				if (clockIt->second == 0) {
					tmpIt++;
					deleteItem(clockIt->first);
					isDeleted = true;
					break;
				} else {
					assert(clockIt->second > 0);
					clockIt->second--;
				}
				clockIt++;
			}
		}
		if (!isDeleted) {
			if (!warmup) { noZeros++;}
			assert(clockLru.size() > 0);
			tmpIt++;
			deleteItem(clockIt->first);
			break;
		}				
		clockIt = (tmpIt == clockLru.end()) ? clockLru.begin() : tmpIt;
	}
	
	std::pair<uint32_t,size_t> p;
	p = firstEviction 
			? std::make_pair(newItem.kId, CLOCK_MAX_VALUE)
			: std::make_pair(newItem.kId, (size_t) 0);
	clockLru.emplace_front(p);
	if (clockLru.size() == 1) {<commit_msg>Fixed few bugs in clock policy<commit_after>#include <iostream>
#include "clock.h"

size_t CLOCK_MAX_VALUE = 15;

Clock::Clock(stats stat) : 
		policy(stat), 
		clockLru(), 
		allObjects(),
		lruSize(0),
		noZeros(0),
		firstEviction(false),
		clockIt()
{
}

Clock::~Clock() {}

size_t Clock::get_bytes_cached() const { return lruSize; }

size_t Clock::proc(const request* r, bool warmup) {
	if (!warmup) {stat.accesses++;}

	auto searchRKId = allObjects.find(r->kid);
	if (searchRKId != allObjects.end()) {
		Clock::ClockItem& item = searchRKId->second;
		ClockLru::iterator& clockItemIt = item.clockLruIt;

		if (r->size() == item.size ) { 
			if (!warmup) {stat.hits++;}
			clockItemIt->second = CLOCK_MAX_VALUE;
			return 1;
		} else {
			if (clockItemIt == clockIt) {
				clockIt++;
				if (clockIt == clockLru.end()) {
					clockIt = clockLru.begin();
				}
			}
			clockLru.erase(clockItemIt);
			lruSize -= item.size;
			allObjects.erase(item.kId);
		}	
	}
	Clock::ClockItem newItem;
	newItem.kId = r->kid;
	newItem.size = r->size();
	
	assert((size_t)newItem.size <= stat.global_mem);

	while (lruSize + newItem.size > stat.global_mem) {	
		if (!firstEviction) { firstEviction = true;}		
		bool isDeleted = false;
		ClockLru::iterator tmpIt, startIt = clockIt;

		while(clockIt != clockLru.end()) {
			assert(clockIt->second <= CLOCK_MAX_VALUE);
			if (clockIt->second == 0) {
				tmpIt = clockIt;
				tmpIt++;
				deleteItem(clockIt->first);
				isDeleted = true;
				break;
			} else {
				clockIt->second--;
			}
			clockIt++;
		}
		if (!isDeleted) {
			clockIt = clockLru.begin();
			assert(clockIt->second <= CLOCK_MAX_VALUE);
			while (clockIt != startIt) {
				if (clockIt->second == 0) {
					tmpIt = clockIt;
					tmpIt++;
					deleteItem(clockIt->first);
					isDeleted = true;
					break;
				} else {
					clockIt->second--;
				}
				clockIt++;
			}
		}
		if (!isDeleted) {
			if (!warmup) { noZeros++;}
			assert(clockLru.size() > 0);
			assert(clockIt != clockLru.end());
			tmpIt = clockIt;
			tmpIt++;
			deleteItem(clockIt->first);
		}				
		clockIt = (tmpIt == clockLru.end()) ? clockLru.begin() : tmpIt;
	}
	
	std::pair<uint32_t,size_t> p;
	p = firstEviction 
			? std::make_pair(newItem.kId, CLOCK_MAX_VALUE)
			: std::make_pair(newItem.kId, (size_t) 0);
	clockLru.emplace_front(p);
	if (clockLru.size() == 1) {
{'fraction_non_alphanumeric': 0.17033089051743283, 'fraction_numerical': 0.005773928492116367, 'mean_word_length': 3.385589094449854, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3545778', 'n_tokens_mistral': 2059, 'n_tokens_neox': 1743, 'n_words': 312}