/*
 * 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 <iostream>
#include <fstream>

#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
#include <limits.h>
#include "../lib.h"

#include "device.h"
#include "i915-gpu.h"
#include "../parameters/parameters.h"
#include "../process/powerconsumer.h"
#include "gpu_rapl_device.h"

extern "C" {
#include <unistd.h>
#include <tracefs.h>
}

i915gpu::i915gpu(): device()
{
	index = get_param_index("gpu-operations");
	rindex = get_result_index("gpu-operations");
}

void i915gpu::start_measurement(void)
{
}

void i915gpu::end_measurement(void)
{
}


double i915gpu::utilization(void) const
{
	return  get_result_value(rindex);

}

void create_i915_gpu(void)
{
	class i915gpu *gpu;
	gpu_rapl_device *rapl_dev;

	if (!tracefs_event_file_exists(nullptr, "i915", "i915_gem_ring_dispatch", "format")) {
		/* try an older tracepoint */
		if (!tracefs_event_file_exists(nullptr, "i915", "i915_gem_request_submit", "format"))
			return;
	}

	register_parameter("gpu-operations");

	gpu = new i915gpu();
	all_devices.push_back(gpu);

	rapl_dev = new gpu_rapl_device(gpu);
	if (rapl_dev->device_present())
		all_devices.push_back(rapl_dev);
	else
		delete rapl_dev;
}



double i915gpu::power_usage(struct result_bundle *result, struct parameter_bundle *bundle)
{
	double power;
	double factor;
	double util;
	double child_power;

	power = 0;
	factor = get_parameter_value(index, bundle);
	util = get_result_value(rindex, result);

	power += util * factor / 100.0;
	for (auto *child : child_devices) {
		child_power = child->power_usage(result, bundle);
		if ((power - child_power) > 0.0)
			power -= child_power;
	}

	return power;
}

void i915gpu::collect_json_fields(std::string &_js)
{
    device::collect_json_fields(_js);
    JSON_FIELD(index);
    JSON_FIELD(rindex);
    JSON_ARRAY("child_devices", child_devices);
}
