1N/A/*
1N/A * Copyright 2009, Intel Corporation
2N/A * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved.
1N/A *
1N/A * This file is part of PowerTOP
1N/A *
1N/A * This program file is free software; you can redistribute it and/or modify it
1N/A * under the terms of the GNU General Public License as published by the
1N/A * Free Software Foundation; version 2 of the License.
1N/A *
1N/A * This program is distributed in the hope that it will be useful, but WITHOUT
1N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1N/A * for more details.
1N/A *
1N/A * You should have received a copy of the GNU General Public License
1N/A * along with this program in a file named COPYING; if not, write to the
1N/A * Free Software Foundation, Inc.,
1N/A * 51 Franklin Street, Fifth Floor,
1N/A * Boston, MA 02110-1301 USA
1N/A *
1N/A * Authors:
1N/A * Arjan van de Ven <arjan@linux.intel.com>
1N/A * Eric C Saxe <eric.saxe@sun.com>
1N/A * Aubrey Li <aubrey.li@intel.com>
1N/A */
1N/A
1N/A/*
1N/A * GPL Disclaimer
1N/A *
1N/A * For the avoidance of doubt, except that if any license choice other
1N/A * than GPL or LGPL is available it will apply instead, Sun elects to
1N/A * use only the General Public License version 2 (GPLv2) at this time
1N/A * for any software where a choice of GPL license versions is made
1N/A * available with the language indicating that GPLv2 or any later
1N/A * version may be used, or where a choice of which version of the GPL
1N/A * is applied is otherwise unspecified.
1N/A */
1N/A
1N/A#include <stdlib.h>
1N/A#include <string.h>
1N/A#include <dtrace.h>
1N/A#include <kstat.h>
1N/A#include <errno.h>
1N/A#include "powertop.h"
1N/A
1N/A/*
1N/A * Global turbo related variables definitions
1N/A */
1N/Aboolean_t g_turbo_supported;
1N/Adouble g_turbo_ratio;
1N/A
1N/A/*
1N/A * The variables to store kstat snapshot
1N/A */
1N/Astatic turbo_info_t *cpu_turbo_info = NULL;
1N/Astatic turbo_info_t *t_new = NULL;
1N/A
1N/A/*
1N/A * Perform setup necessary to enumerate and track CPU turbo information
1N/A */
1N/Astatic int
1N/Apt_turbo_init(void)
1N/A{
1N/A kstat_t *ksp;
1N/A kstat_named_t *knp;
1N/A
1N/A /*
1N/A * check if the CPU turbo is supported
1N/A */
1N/A
2N/A ksp = kstat_lookup(g_kc, "turbo", 0, NULL);
1N/A if (ksp == NULL) {
1N/A g_turbo_supported = B_FALSE;
1N/A return (-1);
1N/A }
1N/A
2N/A (void) kstat_read(g_kc, ksp, NULL);
1N/A
2N/A knp = pt_kstat_data_lookup(ksp, "turbo_supported",
2N/A &g_turbo_supported_index);
1N/A if (knp == NULL) {
1N/A pt_error("couldn't find 'turbo_supported' kstat\n");
1N/A g_turbo_supported = B_FALSE;
1N/A return (-2);
1N/A }
1N/A
1N/A /*
1N/A * Initialize turbo information structure if turbo mode is supported
1N/A */
1N/A if (knp->value.ui32) {
1N/A g_turbo_supported = B_TRUE;
1N/A cpu_turbo_info = calloc((size_t)g_ncpus, sizeof (turbo_info_t));
1N/A t_new = calloc((size_t)g_ncpus, sizeof (turbo_info_t));
1N/A }
1N/A
1N/A return (0);
1N/A}
1N/A
1N/A/*
1N/A * Take a snapshot of each CPU's turbo information
1N/A * by looking through the turbo kstats.
1N/A */
1N/Astatic int
1N/Apt_turbo_snapshot(turbo_info_t *turbo_snapshot)
1N/A{
1N/A kstat_t *ksp;
1N/A kstat_named_t *knp;
1N/A int cpu;
1N/A turbo_info_t *turbo_info;
1N/A
1N/A for (cpu = 0; cpu < g_ncpus; cpu++) {
1N/A turbo_info = &turbo_snapshot[cpu];
2N/A ksp = kstat_lookup(g_kc, "turbo", g_cpu_table[cpu], NULL);
1N/A if (ksp == NULL) {
1N/A pt_error("couldn't find 'turbo' kstat for CPU %d\n",
1N/A cpu);
1N/A return (-1);
1N/A }
1N/A
2N/A if (kstat_read(g_kc, ksp, NULL) == -1) {
1N/A pt_error("couldn't read 'turbo' kstat for CPU %d\n",
1N/A cpu);
1N/A return (-2);
1N/A }
1N/A
2N/A knp = pt_kstat_data_lookup(ksp, "turbo_mcnt",
2N/A &g_turbo_mcnt_index);
1N/A if (knp == NULL) {
1N/A pt_error("couldn't find 'turbo_mcnt' kstat for CPU "
1N/A "%d\n", cpu);
1N/A return (-3);
1N/A }
1N/A
1N/A /*
1N/A * snapshot IA32_MPERF_MSR
1N/A */
1N/A turbo_info->t_mcnt = knp->value.ui64;
1N/A
2N/A knp = pt_kstat_data_lookup(ksp, "turbo_acnt",
2N/A &g_turbo_acnt_index);
1N/A if (knp == NULL) {
1N/A pt_error("couldn't find 'turbo_acnt' kstat for CPU "
1N/A "%d\n", cpu);
1N/A return (-4);
1N/A }
1N/A
1N/A /*
1N/A * snapshot IA32_APERF_MSR
1N/A */
1N/A turbo_info->t_acnt = knp->value.ui64;
1N/A }
1N/A
1N/A return (0);
1N/A}
1N/A
1N/A/*
1N/A * Turbo support checking and information initialization
1N/A */
1N/Aint
1N/Apt_turbo_stat_prepare(void)
1N/A{
1N/A int ret = 0;
1N/A
1N/A if ((ret = pt_turbo_init()) != 0)
1N/A return (ret);
1N/A
1N/A if ((ret = pt_turbo_snapshot(cpu_turbo_info)) != 0)
1N/A pt_error("failed to snapshot 'turbo' kstat\n");
1N/A
1N/A return (ret);
1N/A}
1N/A
1N/A/*
1N/A * When doing the statistics collection, we compare two kstat snapshot
1N/A * and get a delta. the final ratio of performance boost will be worked
1N/A * out according to the kstat delta
1N/A */
1N/Aint
1N/Apt_turbo_stat_collect(void)
1N/A{
1N/A int cpu;
1N/A uint64_t delta_mcnt, delta_acnt;
1N/A double ratio;
1N/A int ret;
1N/A
1N/A /*
1N/A * Take a snapshot of turbo information to setup turbo_info_t
1N/A * structure
1N/A */
1N/A if ((ret = pt_turbo_snapshot(t_new)) != 0) {
1N/A pt_error("failed to snapshot 'turbo' kstat\n");
1N/A return (ret);
1N/A }
1N/A
1N/A /*
1N/A * Calculate the kstat delta and work out the performance boost ratio
1N/A */
1N/A for (cpu = 0; cpu < g_ncpus; cpu++) {
1N/A delta_mcnt = t_new[cpu].t_mcnt - cpu_turbo_info[cpu].t_mcnt;
1N/A delta_acnt = t_new[cpu].t_acnt - cpu_turbo_info[cpu].t_acnt;
1N/A
1N/A if ((delta_mcnt > delta_acnt) || (delta_mcnt == 0))
1N/A ratio = 1.0;
1N/A else
1N/A ratio = (double)delta_acnt / (double)delta_mcnt;
1N/A g_turbo_ratio += ratio;
1N/A }
1N/A
1N/A g_turbo_ratio = g_turbo_ratio / (double)g_ncpus;
1N/A
1N/A /*
1N/A * Update the structure of the kstat for the next time calculation
1N/A */
1N/A (void) memcpy(cpu_turbo_info, t_new, g_ncpus * (sizeof (turbo_info_t)));
1N/A
1N/A return (0);
1N/A}