/*
 * Copyright 2010, Intel Corporation
 *
 * This file is part of PowerTOP
 *
 * This program file is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program in a file named COPYING; if not, write to the
 * Free Software Foundation, Inc,
 * 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301 USA
 * or just google for it.
 *
 * Authors:
 *	Arjan van de Ven <arjan@linux.intel.com>
 */
#include <map>
#include <utility>

#include <cstdint>
#include <cstdio>
#include <sstream>

#include "timer.h"
#include "../lib.h"
#include "process.h"

#include <string_view>

static bool timer_is_deferred(std::string_view handler)
{
	bool ret = false;
	std::string content = read_file_content("/proc/timer_stats");
	if (content.empty()) {
		return ret;
	}

	std::istringstream stream(content);
	std::string line;
	const std::string handler_str(handler);

	while (std::getline(stream, line)) {
		if (line.find(handler_str) != std::string::npos) {
			ret = (line.find("D,") != std::string::npos);
			if (ret)
				break;
		}
	}
	return ret;
}

timer::timer(unsigned long address) : power_consumer()
{
	handler = kernel_function(address);
	raw_count = 0;
	deferred = timer_is_deferred(handler);
}


static std::map<uint64_t, class timer *> all_timers;
static std::map<unsigned long, uint64_t> running_since;

void timer::fire(uint64_t time, uint64_t timer_struct)
{
	running_since[timer_struct] = time;
}

uint64_t timer::done(uint64_t time, uint64_t timer_struct)
{
	int64_t delta;

	if (running_since.find(timer_struct) == running_since.end())
		return ~0ULL;

	if (running_since[timer_struct] > time)
		return 0;

	delta = time - running_since[timer_struct];

	accumulated_runtime += delta;

	raw_count++;

	return delta;
}

double timer::usage_summary(void) const
{
	const double t = (accumulated_runtime - child_runtime) / 1000000.0 / measurement_time / 10;
	return t;
}

std::string timer::usage_units_summary(void) const
{
	return _("%");
}



void all_timers_to_all_power(void)
{
	for (auto &[addr, t] : all_timers)
		if (t->accumulated_runtime > 0)
			all_power.push_back(t);
}


std::string timer::description(void)
{
	if (child_runtime > accumulated_runtime)
		child_runtime = 0;

	desc = handler;
	return desc;
}


class timer * find_create_timer(uint64_t func)
{
	class timer * timer;
	if (all_timers.find(func) != all_timers.end())
		return all_timers[func];

	timer = new class timer(func);
	all_timers[func] = timer;
	return timer;

}

void clear_timers(void)
{
	std::map<uint64_t, class timer *>::iterator it = all_timers.begin();
	while (it != all_timers.end()) {
		delete it->second;
		all_timers.erase(it);
		it = all_timers.begin();
	}
	running_since.clear();
}

bool timer::is_deferred(void) const
{
	return deferred;
}

void timer::collect_json_fields(std::string &_js) const
{
    power_consumer::collect_json_fields(_js);
    JSON_FIELD(desc);
    JSON_FIELD(handler);
    JSON_FIELD(raw_count);
    JSON_FIELD(deferred);
}
