linux-kernel-cpufreq.c_2

本文深入探讨了CPU频率调节的原理与实现机制,详细解释了如何通过内核API进行频率调整,包括设置频率范围、获取当前频率以及使用不同的频率策略。文章还介绍了通知机制、频率同步处理和频率策略切换等关键概念。

static void update_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu)
{
 int j;

 policy->last_cpu = policy->cpu;
 policy->cpu = cpu;

 for_each_cpu(j, policy->cpus)
  per_cpu(cpufreq_policy_cpu, j) = cpu;

#ifdef CONFIG_CPU_FREQ_TABLE
 cpufreq_frequency_table_update_policy_cpu(policy);
#endif
 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
   CPUFREQ_UPDATE_POLICY_CPU, policy);
}

/**
 * __cpufreq_remove_dev - remove a CPU device
 *
 * Removes the cpufreq interface for a CPU device.
 * Caller should already have policy_rwsem in write mode for this CPU.
 * This routine frees the rwsem before returning.
 */
static int __cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
{
 unsigned int cpu = dev->id, ret, cpus;
 unsigned long flags;
 struct cpufreq_policy *data;
 struct kobject *kobj;
 struct completion *cmp;
 struct device *cpu_dev;

 pr_debug("%s: unregistering CPU %u\n", __func__, cpu);

 write_lock_irqsave(&cpufreq_driver_lock, flags);

 data = per_cpu(cpufreq_cpu_data, cpu);
 per_cpu(cpufreq_cpu_data, cpu) = NULL;

 write_unlock_irqrestore(&cpufreq_driver_lock, flags);

 if (!data) {
  pr_debug("%s: No cpu_data found\n", __func__);
  return -EINVAL;
 }

 if (cpufreq_driver->target)
  __cpufreq_governor(data, CPUFREQ_GOV_STOP);

#ifdef CONFIG_HOTPLUG_CPU
 if (!cpufreq_driver->setpolicy)
  strncpy(per_cpu(cpufreq_cpu_governor, cpu),
   data->governor->name, CPUFREQ_NAME_LEN);
#endif

 WARN_ON(lock_policy_rwsem_write(cpu));
 cpus = cpumask_weight(data->cpus);

 if (cpus > 1)
  cpumask_clear_cpu(cpu, data->cpus);
 unlock_policy_rwsem_write(cpu);

 if (cpu != data->cpu) {
  sysfs_remove_link(&dev->kobj, "cpufreq");
 } else if (cpus > 1) {
  /* first sibling now owns the new sysfs dir */
  cpu_dev = get_cpu_device(cpumask_first(data->cpus));
  sysfs_remove_link(&cpu_dev->kobj, "cpufreq");
  ret = kobject_move(&data->kobj, &cpu_dev->kobj);
  if (ret) {
   pr_err("%s: Failed to move kobj: %d", __func__, ret);

   WARN_ON(lock_policy_rwsem_write(cpu));
   cpumask_set_cpu(cpu, data->cpus);

   write_lock_irqsave(&cpufreq_driver_lock, flags);
   per_cpu(cpufreq_cpu_data, cpu) = data;
   write_unlock_irqrestore(&cpufreq_driver_lock, flags);

   unlock_policy_rwsem_write(cpu);

   ret = sysfs_create_link(&cpu_dev->kobj, &data->kobj,
     "cpufreq");
   return -EINVAL;
  }

  WARN_ON(lock_policy_rwsem_write(cpu));
  update_policy_cpu(data, cpu_dev->id);
  unlock_policy_rwsem_write(cpu);
  pr_debug("%s: policy Kobject moved to cpu: %d from: %d\n",
    __func__, cpu_dev->id, cpu);
 }

 /* If cpu is last user of policy, free policy */
 if (cpus == 1) {
  if (cpufreq_driver->target)
   __cpufreq_governor(data, CPUFREQ_GOV_POLICY_EXIT);

  lock_policy_rwsem_read(cpu);
  kobj = &data->kobj;
  cmp = &data->kobj_unregister;
  unlock_policy_rwsem_read(cpu);
  kobject_put(kobj);

  /* we need to make sure that the underlying kobj is actually
   * not referenced anymore by anybody before we proceed with
   * unloading.
   */
  pr_debug("waiting for dropping of refcount\n");
  wait_for_completion(cmp);
  pr_debug("wait complete\n");

  if (cpufreq_driver->exit)
   cpufreq_driver->exit(data);

  free_cpumask_var(data->related_cpus);
  free_cpumask_var(data->cpus);
  kfree(data);
 } else {
  pr_debug("%s: removing link, cpu: %d\n", __func__, cpu);
  cpufreq_cpu_put(data);
  if (cpufreq_driver->target) {
   __cpufreq_governor(data, CPUFREQ_GOV_START);
   __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
  }
 }

 per_cpu(cpufreq_policy_cpu, cpu) = -1;
#ifdef CONFIG_HISI_RDR
 if (g_rdr_cpu_on_off_hook != NULL)
  g_rdr_cpu_on_off_hook(cpu, 0xff, 0);
#endif
 return 0;
}


static int cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
{
 unsigned int cpu = dev->id;
 int retval;

 if (cpu_is_offline(cpu))
  return 0;

 retval = __cpufreq_remove_dev(dev, sif);
 return retval;
}


static void handle_update(struct work_struct *work)
{
 struct cpufreq_policy *policy =
  container_of(work, struct cpufreq_policy, update);
 unsigned int cpu = policy->cpu;
 pr_debug("handle_update for cpu %u called\n", cpu);
 cpufreq_update_policy(cpu);
}

/**
 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're in deep trouble.
 * @cpu: cpu number
 * @old_freq: CPU frequency the kernel thinks the CPU runs at
 * @new_freq: CPU frequency the CPU actually runs at
 *
 * We adjust to current frequency first, and need to clean up later.
 * So either call to cpufreq_update_policy() or schedule handle_update()).
 */
static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq,
    unsigned int new_freq)
{
 struct cpufreq_policy *policy;
 struct cpufreq_freqs freqs;
 unsigned long flags;


 pr_debug("Warning: CPU frequency out of sync: cpufreq and timing "
        "core thinks of %u, is %u kHz.\n", old_freq, new_freq);

 freqs.old = old_freq;
 freqs.new = new_freq;

 read_lock_irqsave(&cpufreq_driver_lock, flags);
 policy = per_cpu(cpufreq_cpu_data, cpu);
 read_unlock_irqrestore(&cpufreq_driver_lock, flags);

 cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
 cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
}


/**
 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
 * @cpu: CPU number
 *
 * This is the last known freq, without actually getting it from the driver.
 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
 */
unsigned int cpufreq_quick_get(unsigned int cpu)
{
 struct cpufreq_policy *policy;
 unsigned int ret_freq = 0;

 if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get)
  return cpufreq_driver->get(cpu);

 policy = cpufreq_cpu_get(cpu);
 if (policy) {
  ret_freq = policy->cur;
  cpufreq_cpu_put(policy);
 }

 return ret_freq;
}
EXPORT_SYMBOL(cpufreq_quick_get);

/**
 * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
 * @cpu: CPU number
 *
 * Just return the max possible frequency for a given CPU.
 */
unsigned int cpufreq_quick_get_max(unsigned int cpu)
{
 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
 unsigned int ret_freq = 0;

 if (policy) {
  ret_freq = policy->max;
  cpufreq_cpu_put(policy);
 }

 return ret_freq;
}
EXPORT_SYMBOL(cpufreq_quick_get_max);


static unsigned int __cpufreq_get(unsigned int cpu)
{
 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
 unsigned int ret_freq = 0;

 if (!cpufreq_driver->get)
  return ret_freq;

 ret_freq = cpufreq_driver->get(cpu);

 if (ret_freq && policy->cur &&
  !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
  /* verify no discrepancy between actual and
     saved value exists */
  if (unlikely(ret_freq != policy->cur)) {
   cpufreq_out_of_sync(cpu, policy->cur, ret_freq);
   schedule_work(&policy->update);
  }
 }

 return ret_freq;
}

/**
 * cpufreq_get - get the current CPU frequency (in kHz)
 * @cpu: CPU number
 *
 * Get the CPU current (static) CPU frequency
 */
unsigned int cpufreq_get(unsigned int cpu)
{
 unsigned int ret_freq = 0;
 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);

 if (!policy)
  goto out;

 if (unlikely(lock_policy_rwsem_read(cpu)))
  goto out_policy;

 ret_freq = __cpufreq_get(cpu);

 unlock_policy_rwsem_read(cpu);

out_policy:
 cpufreq_cpu_put(policy);
out:
 return ret_freq;
}
EXPORT_SYMBOL(cpufreq_get);

static struct subsys_interface cpufreq_interface = {
 .name  = "cpufreq",
 .subsys  = &cpu_subsys,
 .add_dev = cpufreq_add_dev,
 .remove_dev = cpufreq_remove_dev,
};


/**
 * cpufreq_bp_suspend - Prepare the boot CPU for system suspend.
 *
 * This function is only executed for the boot processor.  The other CPUs
 * have been put offline by means of CPU hotplug.
 */
static int cpufreq_bp_suspend(void)
{
 int ret = 0;

 int cpu = smp_processor_id();
 struct cpufreq_policy *cpu_policy;

 pr_debug("suspending cpu %u\n", cpu);

 /* If there's no policy for the boot CPU, we have nothing to do. */
 cpu_policy = cpufreq_cpu_get(cpu);
 if (!cpu_policy)
  return 0;

 if (cpufreq_driver->suspend) {
  ret = cpufreq_driver->suspend(cpu_policy);
  if (ret)
   printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
     "step on CPU %u\n", cpu_policy->cpu);
 }

 cpufreq_cpu_put(cpu_policy);
 return ret;
}

/**
 * cpufreq_bp_resume - Restore proper frequency handling of the boot CPU.
 *
 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
 * 2.) schedule call cpufreq_update_policy() ASAP as interrupts are
 *     restored. It will verify that the current freq is in sync with
 *     what we believe it to be. This is a bit later than when it
 *     should be, but nonethteless it's better than calling
 *     cpufreq_driver->get() here which might re-enable interrupts...
 *
 * This function is only executed for the boot CPU.  The other CPUs have not
 * been turned on yet.
 */
static void cpufreq_bp_resume(void)
{
 int ret = 0;

 int cpu = smp_processor_id();
 struct cpufreq_policy *cpu_policy;

 pr_debug("resuming cpu %u\n", cpu);

 /* If there's no policy for the boot CPU, we have nothing to do. */
 cpu_policy = cpufreq_cpu_get(cpu);
 if (!cpu_policy)
  return;

 if (cpufreq_driver->resume) {
  ret = cpufreq_driver->resume(cpu_policy);
  if (ret) {
   printk(KERN_ERR "cpufreq: resume failed in ->resume "
     "step on CPU %u\n", cpu_policy->cpu);
   goto fail;
  }
 }

 schedule_work(&cpu_policy->update);

fail:
 cpufreq_cpu_put(cpu_policy);
}

static struct syscore_ops cpufreq_syscore_ops = {
 .suspend = cpufreq_bp_suspend,
 .resume  = cpufreq_bp_resume,
};

/**
 * cpufreq_get_current_driver - return current driver's name
 *
 * Return the name string of the currently loaded cpufreq driver
 * or NULL, if none.
 */
const char *cpufreq_get_current_driver(void)
{
 if (cpufreq_driver)
  return cpufreq_driver->name;

 return NULL;
}
EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);

/*********************************************************************
 *                     NOTIFIER LISTS INTERFACE                      *
 *********************************************************************/

/**
 * cpufreq_register_notifier - register a driver with cpufreq
 * @nb: notifier function to register
 *      @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
 *
 * Add a driver to one of two lists: either a list of drivers that
 *      are notified about clock rate changes (once before and once after
 *      the transition), or a list of drivers that are notified about
 *      changes in cpufreq policy.
 *
 * This function may sleep, and has the same return conditions as
 * blocking_notifier_chain_register.
 */
int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
{
 int ret;

 if (cpufreq_disabled())
  return -EINVAL;

 WARN_ON(!init_cpufreq_transition_notifier_list_called);

 switch (list) {
 case CPUFREQ_TRANSITION_NOTIFIER:
  ret = srcu_notifier_chain_register(
    &cpufreq_transition_notifier_list, nb);
  break;
 case CPUFREQ_POLICY_NOTIFIER:
  ret = blocking_notifier_chain_register(
    &cpufreq_policy_notifier_list, nb);
  break;
 default:
  ret = -EINVAL;
 }

 return ret;
}
EXPORT_SYMBOL(cpufreq_register_notifier);


/**
 * cpufreq_unregister_notifier - unregister a driver with cpufreq
 * @nb: notifier block to be unregistered
 *      @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
 *
 * Remove a driver from the CPU frequency notifier list.
 *
 * This function may sleep, and has the same return conditions as
 * blocking_notifier_chain_unregister.
 */
int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
{
 int ret;

 if (cpufreq_disabled())
  return -EINVAL;

 switch (list) {
 case CPUFREQ_TRANSITION_NOTIFIER:
  ret = srcu_notifier_chain_unregister(
    &cpufreq_transition_notifier_list, nb);
  break;
 case CPUFREQ_POLICY_NOTIFIER:
  ret = blocking_notifier_chain_unregister(
    &cpufreq_policy_notifier_list, nb);
  break;
 default:
  ret = -EINVAL;
 }

 return ret;
}
EXPORT_SYMBOL(cpufreq_unregister_notifier);


/*********************************************************************
 *                              GOVERNORS                            *
 *********************************************************************/


int __cpufreq_driver_target(struct cpufreq_policy *policy,
       unsigned int target_freq,
       unsigned int relation)
{
 int retval = -EINVAL;
 unsigned int old_target_freq = target_freq;

 if (cpufreq_disabled())
  return -ENODEV;

 /* Make sure that target_freq is within supported range */
 if (target_freq > policy->max)
  target_freq = policy->max;
 if (target_freq < policy->min)
  target_freq = policy->min;

 pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
   policy->cpu, target_freq, relation, old_target_freq);

 if (target_freq == policy->cur)
  return 0;

 if (cpufreq_driver->target)
  retval = cpufreq_driver->target(policy, target_freq, relation);

 return retval;
}
EXPORT_SYMBOL_GPL(__cpufreq_driver_target);

int cpufreq_driver_target(struct cpufreq_policy *policy,
     unsigned int target_freq,
     unsigned int relation)
{
 int ret = -EINVAL;

 policy = cpufreq_cpu_get(policy->cpu);
 if (!policy)
  goto no_policy;

 if (unlikely(lock_policy_rwsem_write(policy->cpu)))
  goto fail;

 ret = __cpufreq_driver_target(policy, target_freq, relation);

 unlock_policy_rwsem_write(policy->cpu);

fail:
 cpufreq_cpu_put(policy);
no_policy:
 return ret;
}
EXPORT_SYMBOL_GPL(cpufreq_driver_target);

int __cpufreq_driver_getavg(struct cpufreq_policy *policy, unsigned int cpu)
{
 int ret = 0;

 if (cpufreq_disabled())
  return ret;

 if (!cpufreq_driver->getavg)
  return 0;

 policy = cpufreq_cpu_get(policy->cpu);
 if (!policy)
  return -EINVAL;

 ret = cpufreq_driver->getavg(policy, cpu);

 cpufreq_cpu_put(policy);
 return ret;
}
EXPORT_SYMBOL_GPL(__cpufreq_driver_getavg);

/*
 * when "event" is CPUFREQ_GOV_LIMITS
 */

static int __cpufreq_governor(struct cpufreq_policy *policy,
     unsigned int event)
{
 int ret;

 /* Only must be defined when default governor is known to have latency
    restrictions, like e.g. conservative or ondemand.
    That this is the case is already ensured in Kconfig
 */
#ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
 struct cpufreq_governor *gov = &cpufreq_gov_performance;
#else
 struct cpufreq_governor *gov = NULL;
#endif

 if (policy->governor->max_transition_latency &&
     policy->cpuinfo.transition_latency >
     policy->governor->max_transition_latency) {
  if (!gov)
   return -EINVAL;
  else {
   printk(KERN_WARNING "%s governor failed, too long"
          " transition latency of HW, fallback"
          " to %s governor\n",
          policy->governor->name,
          gov->name);
   policy->governor = gov;
  }
 }

 if (!try_module_get(policy->governor->owner))
  return -EINVAL;

 pr_debug("__cpufreq_governor for CPU %u, event %u\n",
      policy->cpu, event);

 mutex_lock(&cpufreq_governor_lock);
 if ((!policy->governor_enabled && (event == CPUFREQ_GOV_STOP)) ||
     (policy->governor_enabled && (event == CPUFREQ_GOV_START))) {
  mutex_unlock(&cpufreq_governor_lock);
  return -EBUSY;
 }

 if (event == CPUFREQ_GOV_STOP)
  policy->governor_enabled = false;
 else if (event == CPUFREQ_GOV_START)
  policy->governor_enabled = true;

 mutex_unlock(&cpufreq_governor_lock);

 ret = policy->governor->governor(policy, event);

 if (!ret) {
  if (event == CPUFREQ_GOV_POLICY_INIT)
   policy->governor->initialized++;
  else if (event == CPUFREQ_GOV_POLICY_EXIT)
   policy->governor->initialized--;
 } else {
  /* Restore original values */
  mutex_lock(&cpufreq_governor_lock);
  if (event == CPUFREQ_GOV_STOP)
   policy->governor_enabled = true;
  else if (event == CPUFREQ_GOV_START)
   policy->governor_enabled = false;
  mutex_unlock(&cpufreq_governor_lock);
 }

 /* we keep one module reference alive for
   each CPU governed by this CPU */
 if ((event != CPUFREQ_GOV_START) || ret)
  module_put(policy->governor->owner);
 if ((event == CPUFREQ_GOV_STOP) && !ret)
  module_put(policy->governor->owner);

 return ret;
}


int cpufreq_register_governor(struct cpufreq_governor *governor)
{
 int err;

 if (!governor)
  return -EINVAL;

 if (cpufreq_disabled())
  return -ENODEV;

 mutex_lock(&cpufreq_governor_mutex);

 governor->initialized = 0;
 err = -EBUSY;
 if (__find_governor(governor->name) == NULL) {
  err = 0;
  list_add(&governor->governor_list, &cpufreq_governor_list);
 }

 mutex_unlock(&cpufreq_governor_mutex);
 return err;
}
EXPORT_SYMBOL_GPL(cpufreq_register_governor);


void cpufreq_unregister_governor(struct cpufreq_governor *governor)
{
#ifdef CONFIG_HOTPLUG_CPU
 int cpu;
#endif

 if (!governor)
  return;

 if (cpufreq_disabled())
  return;

#ifdef CONFIG_HOTPLUG_CPU
 for_each_present_cpu(cpu) {
  if (cpu_online(cpu))
   continue;
  if (!strcmp(per_cpu(cpufreq_cpu_governor, cpu), governor->name))
   strcpy(per_cpu(cpufreq_cpu_governor, cpu), "\0");
 }
#endif

 mutex_lock(&cpufreq_governor_mutex);
 list_del(&governor->governor_list);
 mutex_unlock(&cpufreq_governor_mutex);
 return;
}
EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);

 

/*********************************************************************
 *                          POLICY INTERFACE                         *
 *********************************************************************/

/**
 * cpufreq_get_policy - get the current cpufreq_policy
 * @policy: struct cpufreq_policy into which the current cpufreq_policy
 * is written
 *
 * Reads the current cpufreq policy.
 */
int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
{
 struct cpufreq_policy *cpu_policy;
 if (!policy)
  return -EINVAL;

 cpu_policy = cpufreq_cpu_get(cpu);
 if (!cpu_policy)
  return -EINVAL;

 memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy));

 cpufreq_cpu_put(cpu_policy);
 return 0;
}
EXPORT_SYMBOL(cpufreq_get_policy);


/*
 * data   : current policy.
 * policy : policy to be set.
 */
static int __cpufreq_set_policy(struct cpufreq_policy *data,
    struct cpufreq_policy *policy)
{
 int ret = 0, failed = 1;

 pr_debug("setting new policy for CPU %u: %u - %u kHz\n", policy->cpu,
  policy->min, policy->max);

 memcpy(&policy->cpuinfo, &data->cpuinfo,
    sizeof(struct cpufreq_cpuinfo));

 if (policy->min > data->max || policy->max < data->min) {
  ret = -EINVAL;
  goto error_out;
 }

 /* verify the cpu speed can be set within this limit */
 ret = cpufreq_driver->verify(policy);
 if (ret)
  goto error_out;

 /* adjust if necessary - all reasons */
 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
   CPUFREQ_ADJUST, policy);

 /* adjust if necessary - hardware incompatibility*/
 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
   CPUFREQ_INCOMPATIBLE, policy);

 /* verify the cpu speed can be set within this limit,
    which might be different to the first one */
 ret = cpufreq_driver->verify(policy);
 if (ret)
  goto error_out;

 /* notification of the new policy */
 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
   CPUFREQ_NOTIFY, policy);

 data->min = policy->min;
 data->max = policy->max;

 pr_debug("new min and max freqs are %u - %u kHz\n",
     data->min, data->max);

 if (cpufreq_driver->setpolicy) {
  data->policy = policy->policy;
  pr_debug("setting range\n");
  ret = cpufreq_driver->setpolicy(policy);
 } else {
  if (policy->governor != data->governor) {
   /* save old, working values */
   struct cpufreq_governor *old_gov = data->governor;

   pr_debug("governor switch\n");

   /* end old governor */
   if (data->governor) {
    __cpufreq_governor(data, CPUFREQ_GOV_STOP);
    unlock_policy_rwsem_write(policy->cpu);
    __cpufreq_governor(data,
      CPUFREQ_GOV_POLICY_EXIT);
    lock_policy_rwsem_write(policy->cpu);
   }

   /* start new governor */
   data->governor = policy->governor;
   if (!__cpufreq_governor(data, CPUFREQ_GOV_POLICY_INIT)) {
    if (!__cpufreq_governor(data, CPUFREQ_GOV_START)) {
     failed = 0;
    } else {
     unlock_policy_rwsem_write(policy->cpu);
     __cpufreq_governor(data,
       CPUFREQ_GOV_POLICY_EXIT);
     lock_policy_rwsem_write(policy->cpu);
    }
   }

   if (failed) {
    /* new governor failed, so re-start old one */
    pr_debug("starting governor %s failed\n",
       data->governor->name);
    if (old_gov) {
     data->governor = old_gov;
     __cpufreq_governor(data,
       CPUFREQ_GOV_POLICY_INIT);
     __cpufreq_governor(data,
          CPUFREQ_GOV_START);
    }
    ret = -EINVAL;
    goto error_out;
   }
   /* might be a policy change, too, so fall through */
  }
  pr_debug("governor: change or update limits\n");
  __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
 }

error_out:
 return ret;
}

/**
 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
 * @cpu: CPU which shall be re-evaluated
 *
 * Useful for policy notifiers which have different necessities
 * at different times.
 */
int cpufreq_update_policy(unsigned int cpu)
{
 struct cpufreq_policy *data = cpufreq_cpu_get(cpu);
 struct cpufreq_policy policy;
 int ret;

 if (!data) {
  ret = -ENODEV;
  goto no_policy;
 }

 if (unlikely(lock_policy_rwsem_write(cpu))) {
  ret = -EINVAL;
  goto fail;
 }

 pr_debug("updating policy for CPU %u\n", cpu);
 memcpy(&policy, data, sizeof(struct cpufreq_policy));
 policy.min = data->user_policy.min;
 policy.max = data->user_policy.max;
 policy.policy = data->user_policy.policy;
 policy.governor = data->user_policy.governor;

 /* BIOS might change freq behind our back
   -> ask driver for current freq and notify governors about a change */
 if (cpufreq_driver->get) {
  policy.cur = cpufreq_driver->get(cpu);
  if (!data->cur) {
   pr_debug("Driver did not initialize current freq");
   data->cur = policy.cur;
  } else {
   if (data->cur != policy.cur && cpufreq_driver->target)
    cpufreq_out_of_sync(cpu, data->cur,
        policy.cur);
  }
 }

 ret = __cpufreq_set_policy(data, &policy);

 unlock_policy_rwsem_write(cpu);

fail:
 cpufreq_cpu_put(data);
no_policy:
 return ret;
}
EXPORT_SYMBOL(cpufreq_update_policy);

static int __cpuinit cpufreq_cpu_callback(struct notifier_block *nfb,
     unsigned long action, void *hcpu)
{
 unsigned int cpu = (unsigned long)hcpu;
 struct device *dev;

 dev = get_cpu_device(cpu);
 if (dev) {
  switch (action) {
  case CPU_ONLINE:
  case CPU_ONLINE_FROZEN:
   cpufreq_add_dev(dev, NULL);
   kobject_uevent(&dev->kobj, KOBJ_ADD);
   break;
  case CPU_DOWN_PREPARE:
  case CPU_DOWN_PREPARE_FROZEN:
   __cpufreq_remove_dev(dev, NULL);
   break;
  case CPU_DOWN_FAILED:
  case CPU_DOWN_FAILED_FROZEN:
   cpufreq_add_dev(dev, NULL);
   break;
  }
 }
 return NOTIFY_OK;
}

static struct notifier_block __refdata cpufreq_cpu_notifier = {
    .notifier_call = cpufreq_cpu_callback,
};

/*********************************************************************
 *               REGISTER / UNREGISTER CPUFREQ DRIVER                *
 *********************************************************************/

/**
 * cpufreq_register_driver - register a CPU Frequency driver
 * @driver_data: A struct cpufreq_driver containing the values#
 * submitted by the CPU Frequency driver.
 *
 *   Registers a CPU Frequency driver to this core code. This code
 * returns zero on success, -EBUSY when another driver got here first
 * (and isn't unregistered in the meantime).
 *
 */
int cpufreq_register_driver(struct cpufreq_driver *driver_data)
{
 unsigned long flags;
 int ret;

 if (cpufreq_disabled())
  return -ENODEV;

 if (!driver_data || !driver_data->verify || !driver_data->init ||
     ((!driver_data->setpolicy) && (!driver_data->target)))
  return -EINVAL;

 pr_debug("trying to register driver %s\n", driver_data->name);

 if (driver_data->setpolicy)
  driver_data->flags |= CPUFREQ_CONST_LOOPS;

 write_lock_irqsave(&cpufreq_driver_lock, flags);
 if (cpufreq_driver) {
  write_unlock_irqrestore(&cpufreq_driver_lock, flags);
  return -EBUSY;
 }
 cpufreq_driver = driver_data;
 write_unlock_irqrestore(&cpufreq_driver_lock, flags);

 ret = subsys_interface_register(&cpufreq_interface);
 if (ret)
  goto err_null_driver;

 if (!(cpufreq_driver->flags & CPUFREQ_STICKY)) {
  int i;
  ret = -ENODEV;

  /* check for at least one working CPU */
  for (i = 0; i < nr_cpu_ids; i++)
   if (cpu_possible(i) && per_cpu(cpufreq_cpu_data, i)) {
    ret = 0;
    break;
   }

  /* if all ->init() calls failed, unregister */
  if (ret) {
   pr_debug("no CPU initialized for driver %s\n",
       driver_data->name);
   goto err_if_unreg;
  }
 }

 register_hotcpu_notifier(&cpufreq_cpu_notifier);
 pr_debug("driver %s up and running\n", driver_data->name);

 return 0;
err_if_unreg:
 subsys_interface_unregister(&cpufreq_interface);
err_null_driver:
 write_lock_irqsave(&cpufreq_driver_lock, flags);
 cpufreq_driver = NULL;
 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
 return ret;
}
EXPORT_SYMBOL_GPL(cpufreq_register_driver);


/**
 * cpufreq_unregister_driver - unregister the current CPUFreq driver
 *
 *    Unregister the current CPUFreq driver. Only call this if you have
 * the right to do so, i.e. if you have succeeded in initialising before!
 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
 * currently not initialised.
 */
int cpufreq_unregister_driver(struct cpufreq_driver *driver)
{
 unsigned long flags;

 if (!cpufreq_driver || (driver != cpufreq_driver))
  return -EINVAL;

 pr_debug("unregistering driver %s\n", driver->name);

 subsys_interface_unregister(&cpufreq_interface);
 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);

 write_lock_irqsave(&cpufreq_driver_lock, flags);
 cpufreq_driver = NULL;
 write_unlock_irqrestore(&cpufreq_driver_lock, flags);

 return 0;
}
EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);

static int __init cpufreq_core_init(void)
{
 int cpu;

 if (cpufreq_disabled())
  return -ENODEV;

 for_each_possible_cpu(cpu) {
  per_cpu(cpufreq_policy_cpu, cpu) = -1;
  init_rwsem(&per_cpu(cpu_policy_rwsem, cpu));
 }

 cpufreq_global_kobject = kobject_create_and_add("cpufreq", &cpu_subsys.dev_root->kobj);
 BUG_ON(!cpufreq_global_kobject);
 register_syscore_ops(&cpufreq_syscore_ops);

 return 0;
}
core_initcall(cpufreq_core_init);

import tkinter as tk import time import sys import os import platform import ctypes import csv import threading import numpy as np import matplotlib.pyplot as plt from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg from ctypes import windll, wintypes from collections import defaultdict from datetime import datetime # 解决中文警告问题 import matplotlib as mpl mpl.rcParams['font.sans-serif'] = ['SimHei', 'Microsoft YaHei'] # 指定默认字体 mpl.rcParams['axes.unicode_minus'] = False # 解决负号显示问题 class HighPrecisionTimer: """高精度计时器基类""" def __init__(self): self.start_time = 0 self.total_elapsed = 0 self.running = False self.interrupt_count = 0 # 中断计数器 self.core_usage = defaultdict(float) # 各核心使用时间 def start(self): """开始计时""" if not self.running: self.start_time = self.current_time() self.running = True def stop(self): """停止计时""" if self.running: elapsed = self.current_time() - self.start_time self.total_elapsed += elapsed self.update_core_usage(elapsed) # 更新核心使用情况 self.running = False def reset(self): """重置计时器""" self.total_elapsed = 0 self.running = False self.interrupt_count = 0 self.core_usage.clear() def current_elapsed(self): """获取当前已过时间(毫秒)""" if self.running: return self.total_elapsed + (self.current_time() - self.start_time) return self.total_elapsed def current_time(self): """抽象方法,由子类实现具体计时方案""" raise NotImplementedError("Subclasses must implement current_time()") def update_core_usage(self, elapsed): """更新核心使用情况""" # 此处为简化实现,实际项目中应使用系统API获取各核心使用数据 if platform.system() == 'Windows': # 模拟多核使用情况 for core in range(os.cpu_count()): self.core_usage[core] += elapsed * (0.3 + 0.6 * np.random.random()) else: # Linux系统可读取/proc/stat获取实际数据 self.core_usage[0] = elapsed # 默认单核心 def get_cpu_utilization(self, physical_time): if physical_time > 0: return min(100, max(0, (self.current_elapsed() / physical_time) * 100)) return 0 def record_interrupt(self): """记录中断事件""" self.interrupt_count += 1 class PerformanceCounterTimer(HighPrecisionTimer): """基于性能计数器的物理计时方案""" def __init__(self): super().__init__() # 获取性能计数器频率 if platform.system() == 'Windows': self.frequency = ctypes.c_int64() windll.kernel32.QueryPerformanceFrequency(ctypes.byref(self.frequency)) self.frequency = self.frequency.value else: # Linux/macOS使用time.perf_counter self.frequency = 1e9 # 1秒 = 1e9纳秒 def current_time(self): """获取当前性能计数器时间(毫秒)""" if platform.system() == 'Windows': counter = ctypes.c_int64() windll.kernel32.QueryPerformanceCounter(ctypes.byref(counter)) return (counter.value * 1000) / self.frequency # 转换为毫秒 else: # 跨平台方案 return time.perf_counter() * 1000 class CPUCycleTimer(HighPrecisionTimer): """基于CPU周期的计时方案""" def __init__(self): super().__init__() # 获取CPU频率 self.frequency = self.get_cpu_frequency() self.power_model = self.create_power_model() # 创建功耗模型 def get_cpu_frequency(self): """尝试获取CPU频率""" try: if platform.system() == 'Windows': # Windows获取CPU频率 import winreg key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, r"HARDWARE\DESCRIPTION\System\CentralProcessor\0") value, _ = winreg.QueryValueEx(key, "~MHz") winreg.CloseKey(key) return value * 1e6 # MHz转换为Hz else: # Linux/macOS获取CPU频率 with open('/proc/cpuinfo') as f: for line in f: if 'cpu MHz' in line: return float(line.split(':')[1].strip()) * 1e6 # 默认值 return 3.5e9 # 3.5 GHz except: return 3.5e9 # 3.5 GHz def current_time(self): """获取当前CPU周期计数并转换为毫秒""" if platform.system() == 'Windows': return time.perf_counter_ns() / 1_000_000 # 返回毫秒级精确时间 else: # 在实际应用中应使用RDTSC指令 return time.process_time() * 1000 def create_power_model(self): # 简化的线性模型: P = a * utilization + b return lambda u: 15 + 85 * (u/100) # 基础功耗15W + 动态功耗 def estimate_energy(self, physical_time): utilization = self.get_cpu_utilization(physical_time) power = self.power_model(utilization) # 当前功耗(W) time_seconds = physical_time / 1000 # 转换为秒 return power * time_seconds # 能耗(J) class TimerApp: """计时器应用GUI""" def __init__(self, root): self.root = root root.title("高精度计时器 - 专业版") root.geometry("800x600") root.resizable(True, True) # 创建两种计时器实例 self.performance_timer = PerformanceCounterTimer() self.cpu_cycle_timer = CPUCycleTimer() # 当前选中的计时方案 self.current_timer = self.performance_timer self.interrupt_monitor_running = False # 创建UI self.create_widgets() # 性能优化计数器 self.update_count = 0 # 启动UI更新循环 self.update_display() # 启动中断监控线程 self.start_interrupt_monitor() def create_widgets(self): """创建界面组件""" # 主框架 main_frame = tk.Frame(self.root) main_frame.pack(fill=tk.BOTH, expand=True, padx=10, pady=10) # 左侧控制面板 control_frame = tk.Frame(main_frame) control_frame.pack(side=tk.LEFT, fill=tk.Y, padx=5, pady=5) # 方案选择 mode_frame = tk.LabelFrame(control_frame, text="计时方案", padx=10, pady=10) mode_frame.pack(fill=tk.X, pady=5) self.mode_var = tk.StringVar(value="performance") modes = [("性能计数器 (物理时间)", "performance"), ("CPU周期 (CPU时间)", "cpu")] for text, mode in modes: rb = tk.Radiobutton(mode_frame, text=text, variable=self.mode_var, value=mode, command=self.switch_mode) rb.pack(anchor=tk.W, padx=5, pady=2) # 控制按钮 btn_frame = tk.Frame(control_frame) btn_frame.pack(fill=tk.X, pady=10) self.start_btn = tk.Button(btn_frame, text="开始", width=12, command=self.start_stop) self.start_btn.pack(side=tk.LEFT, padx=5) reset_btn = tk.Button(btn_frame, text="重置", width=12, command=self.reset) reset_btn.pack(side=tk.LEFT, padx=5) # 统计按钮 stats_btn = tk.Button(btn_frame, text="生成报告", width=12, command=self.generate_report) stats_btn.pack(side=tk.LEFT, padx=5) # CPU信息 info_frame = tk.LabelFrame(control_frame, text="系统信息", padx=10, pady=10) info_frame.pack(fill=tk.X, pady=5) cpu_freq = self.cpu_cycle_timer.frequency freq_text = f"CPU频率: {cpu_freq/1e9:.2f} GHz" if cpu_freq >= 1e9 else f"CPU频率: {cpu_freq/1e6:.0f} MHz" self.cpu_label = tk.Label(info_frame, text=freq_text, justify=tk.LEFT) self.cpu_label.pack(anchor=tk.W) cores_text = f"CPU核心数: {os.cpu_count()}" tk.Label(info_frame, text=cores_text, justify=tk.LEFT).pack(anchor=tk.W) # 中断统计 interrupt_frame = tk.LabelFrame(control_frame, text="中断统计", padx=10, pady=10) interrupt_frame.pack(fill=tk.X, pady=5) self.interrupt_label = tk.Label(interrupt_frame, text="中断次数: 0") self.interrupt_label.pack(anchor=tk.W) # 右侧数据显示 display_frame = tk.Frame(main_frame) display_frame.pack(side=tk.RIGHT, fill=tk.BOTH, expand=True, padx=5, pady=5) # 双模式时间显示 time_frame = tk.LabelFrame(display_frame, text="时间对比", padx=10, pady=10) time_frame.pack(fill=tk.X, pady=5) # 物理时间显示 - 添加颜色(蓝色) physical_frame = tk.Frame(time_frame) physical_frame.pack(fill=tk.X, pady=5) tk.Label(physical_frame, text="物理时间:", width=10).pack(side=tk.LEFT) self.physical_label = tk.Label(physical_frame, text="00:00:00.000", font=("Consolas", 12), fg="blue") self.physical_label.pack(side=tk.LEFT) # CPU时间显示 - 添加颜色(红色) cpu_frame = tk.Frame(time_frame) cpu_frame.pack(fill=tk.X, pady=5) tk.Label(cpu_frame, text="CPU时间:", width=10).pack(side=tk.LEFT) self.cpu_time_label = tk.Label(cpu_frame, text="00:00:00.000", font=("Consolas", 12), fg="red") self.cpu_time_label.pack(side=tk.LEFT) # 时间分解分析 analysis_frame = tk.LabelFrame(display_frame, text="时间分解分析", padx=10, pady=10) analysis_frame.pack(fill=tk.X, pady=5) # CPU利用率 - 添加颜色(绿色) util_frame = tk.Frame(analysis_frame) util_frame.pack(fill=tk.X, pady=2) tk.Label(util_frame, text="CPU利用率:", width=15).pack(side=tk.LEFT) self.util_label = tk.Label(util_frame, text="0.0%", fg="green") self.util_label.pack(side=tk.LEFT) # 系统等待时间 - 添加颜色(紫色) wait_frame = tk.Frame(analysis_frame) wait_frame.pack(fill=tk.X, pady=2) tk.Label(wait_frame, text="系统等待时间:", width=15).pack(side=tk.LEFT) self.wait_label = tk.Label(wait_frame, text="0.0 ms", fg="purple") self.wait_label.pack(side=tk.LEFT) # 能耗估算 - 添加颜色(橙色) energy_frame = tk.Frame(analysis_frame) energy_frame.pack(fill=tk.X, pady=2) tk.Label(energy_frame, text="能耗估算:", width=15).pack(side=tk.LEFT) self.energy_label = tk.Label(energy_frame, text="0.0 J", fg="orange") self.energy_label.pack(side=tk.LEFT) # 多核热力图 heatmap_frame = tk.LabelFrame(display_frame, text="多核利用率热力图", padx=10, pady=10) heatmap_frame.pack(fill=tk.BOTH, expand=True, pady=5) # 创建热力图 self.figure, self.ax = plt.subplots(figsize=(6, 3)) self.canvas = FigureCanvasTkAgg(self.figure, heatmap_frame) self.canvas.get_tk_widget().pack(fill=tk.BOTH, expand=True) # 初始化热力图对象 self.heatmap_img = None self.heatmap_texts = [] # 初始化热力图 self.init_heatmap() def init_heatmap(self): """初始化热力图对象""" cores = list(range(os.cpu_count())) util = [0] * len(cores) # 创建热力图数据 data = np.array(util).reshape(1, -1) # 创建图像对象 self.heatmap_img = self.ax.imshow(data, cmap='viridis', aspect='auto', vmin=0, vmax=100) # 创建文本标注 self.heatmap_texts = [] for i in range(len(cores)): text = self.ax.text(i, 0, f'{util[i]:.1f}%', ha='center', va='center', color='w') self.heatmap_texts.append(text) self.ax.set_title('CPU核心利用率 (%)') self.ax.set_xticks(range(len(cores))) self.ax.set_xticklabels([f'Core {core}' for core in cores]) self.ax.get_yaxis().set_visible(False) # 添加颜色条(只创建一次) self.figure.colorbar(self.heatmap_img, ax=self.ax) self.canvas.draw() def switch_mode(self): """切换计时方案""" selected = self.mode_var.get() if selected == "performance": self.current_timer = self.performance_timer else: self.current_timer = self.cpu_cycle_timer # 更新CPU频率显示 cpu_freq = self.cpu_cycle_timer.frequency freq_text = f"CPU频率: {cpu_freq/1e9:.2f} GHz" if cpu_freq >= 1e9 else f"CPU频率: {cpu_freq/1e6:.0f} MHz" self.cpu_label.config(text=freq_text) def start_stop(self): """开始/停止计时 - 只操作当前选中的计时器""" if self.current_timer.running: self.current_timer.stop() self.start_btn.config(text="开始") else: # 只启动当前选中的计时器 self.current_timer.start() self.start_btn.config(text="停止") def reset(self): """只重置当前选中的计时器""" self.current_timer.reset() if isinstance(self.current_timer, PerformanceCounterTimer): self.physical_label.config(text="00:00:00.000") else: self.cpu_time_label.config(text="00:00:00.000") self.start_btn.config(text="开始") # 清空性能指标 self.util_label.config(text="0.0%") self.wait_label.config(text="0.0 ms") self.energy_label.config(text="0.0 J") def update_heatmap(self, cores, util): """高效更新热力图""" # 更新图像数据 data = np.array(util).reshape(1, -1) if self.heatmap_img is not None: self.heatmap_img.set_data(data) # 更新文本标注 for i, text in enumerate(self.heatmap_texts): text.set_text(f'{util[i]:.1f}%') # 只绘制变更部分 self.canvas.draw_idle() def start_interrupt_monitor(self): """启动中断监控线程""" if not self.interrupt_monitor_running: self.interrupt_monitor_running = True threading.Thread(target=self.monitor_interrupts, daemon=True).start() def monitor_interrupts(self): """模拟中断监控""" while self.interrupt_monitor_running: time.sleep(0.1) # 在实际应用中,这里应读取系统中断计数器 if self.current_timer.running: # 模拟随机中断 if np.random.random() < 0.02: # 2%概率发生中断 self.performance_timer.record_interrupt() self.cpu_cycle_timer.record_interrupt() self.root.after(0, self.update_interrupt_count) def update_interrupt_count(self): """更新中断计数显示""" count = self.performance_timer.interrupt_count self.interrupt_label.config(text=f"中断次数: {count}") def generate_report(self): """生成详细分析报告""" filename = f"timer_report_{datetime.now().strftime('%Y%m%d_%H%M%S')}.csv" with open(filename, 'w', newline='', encoding='utf-8') as f: writer = csv.writer(f) # 写入报告头部信息 writer.writerow(["高精度计时器分析报告"]) writer.writerow(["生成时间", datetime.now().strftime("%Y-%m-%d %H:%M:%S")]) writer.writerow([]) # 写入系统信息 writer.writerow(["系统信息"]) writer.writerow(["操作系统", platform.platform()]) writer.writerow(["CPU核心数", os.cpu_count()]) cpu_freq = self.cpu_cycle_timer.frequency freq_text = f"{cpu_freq/1e9:.2f} GHz" if cpu_freq >= 1e9 else f"{cpu_freq/1e6:.0f} MHz" writer.writerow(["CPU频率", freq_text]) writer.writerow(["Python版本", sys.version]) writer.writerow([]) # 格式化时间函数 def format_ms(ms): hours, rem = divmod(ms, 3600000) minutes, rem = divmod(rem, 60000) seconds, milliseconds = divmod(rem, 1000) return f"{int(hours):02d}:{int(minutes):02d}:{int(seconds):02d}.{int(milliseconds):03d}" # 写入时间指标 physical_time = self.performance_timer.total_elapsed cpu_time = self.cpu_cycle_timer.total_elapsed wait_time = max(0, physical_time - cpu_time) # 系统等待时间 writer.writerow(["时间指标"]) writer.writerow(["度量项", "数值(毫秒)", "格式化时间"]) writer.writerow(["物理时间", f"{physical_time:.3f}", format_ms(physical_time)]) writer.writerow(["CPU时间", f"{cpu_time:.3f}", format_ms(cpu_time)]) writer.writerow(["系统等待时间", f"{wait_time:.3f}", format_ms(wait_time)]) writer.writerow([]) # 写入性能指标 utilization = self.cpu_cycle_timer.get_cpu_utilization(physical_time) if physical_time > 0 else 0 energy = self.cpu_cycle_timer.estimate_energy(physical_time) if physical_time > 0 else 0 writer.writerow(["性能指标"]) writer.writerow(["CPU利用率", f"{utilization:.2f}%"]) writer.writerow(["系统中断次数", self.performance_timer.interrupt_count]) writer.writerow(["估算能耗", f"{energy:.2f} J"]) writer.writerow([]) # 写入多核分析 writer.writerow(["多核利用率分析"]) writer.writerow(["核心ID", "使用时间(毫秒)", "占比"]) # 计算核心使用占比 cores = sorted(self.cpu_cycle_timer.core_usage.keys()) total_cpu_time = max(1, cpu_time) # 防止除零 for core in cores: core_time = self.cpu_cycle_timer.core_usage.get(core, 0) percentage = (core_time / total_cpu_time) * 100 writer.writerow([f"Core {core}", f"{core_time:.3f}", f"{percentage:.2f}%"]) # 添加数学公式注解 writer.writerow([]) writer.writerow(["计算公式注解"]) writer.writerow(["CPU利用率", r"$\eta = \frac{T_{cpu}}{T_{physical}} \times 100\%$"]) writer.writerow(["系统等待时间", r"$\Delta t = T_{physical} - T_{cpu}$"]) writer.writerow(["能耗估算", r"$E = \int_{0}^{T} P(u(t)) \, dt$", "(P=功耗模型)"]) # 显示成功消息 tk.messagebox.showinfo("报告生成", f"分析报告已保存至:\n{os.path.abspath(filename)}") def update_display(self): """根据当前选中的计时器更新显示""" current_ms = self.current_timer.current_elapsed() # 格式化时间 time_str = self.ms_to_time_str(current_ms) # 更新标签显示 if isinstance(self.current_timer, PerformanceCounterTimer): self.physical_label.config(text=time_str) # CPU时间不显示 self.cpu_time_label.config(text="--:--:--.---") else: self.cpu_time_label.config(text=time_str) # 物理时间不显示 self.physical_label.config(text="--:--:--.---") # 计算并显示性能指标(仅CPU时间模式) if isinstance(self.current_timer, CPUCycleTimer): utilization = self.current_timer.get_cpu_utilization(current_ms) if current_ms > 0 else 0 energy = self.current_timer.estimate_energy(current_ms) if current_ms > 0 else 0 self.util_label.config(text=f"{utilization:.1f}%") self.energy_label.config(text=f"{energy:.1f} J") else: # 物理时间模式下隐藏性能指标 self.util_label.config(text="N/A") self.energy_label.config(text="N/A") # 系统等待时间始终不显示 self.wait_label.config(text="N/A") # 每100毫秒更新一次显示 self.root.after(100, self.update_display) def ms_to_time_str(self, ms): """将毫秒数格式化为 HH:MM:SS.ms 字符串""" ms = max(0, ms) # 确保非负 seconds, milliseconds = divmod(ms, 1000) minutes, seconds = divmod(seconds, 60) hours, minutes = divmod(minutes, 60) return f"{int(hours):02d}:{int(minutes):02d}:{int(seconds):02d}.{int(milliseconds):03d}" def update_heatmap(self, cores, util): """高效更新热力图(优化性能)""" # 创建热力图数据 data = np.array(util).reshape(1, -1) if hasattr(self, 'heatmap_img'): # 更新现有图像数据 self.heatmap_img.set_data(data) # 更新文本标注 for i, text in enumerate(self.heatmap_texts[:len(cores)]): text.set_text(f'{util[i]:.1f}%') else: # 首次创建热力图 self.heatmap_img = self.ax.imshow(data, cmap='viridis', aspect='auto', vmin=0, vmax=100) # 创建文本标注 self.heatmap_texts = [] for i in range(len(cores)): text = self.ax.text(i, 0, f'{util[i]:.1f}%', ha='center', va='center', color='w') self.heatmap_texts.append(text) self.ax.set_title('CPU核心利用率 (%)') self.ax.set_xticks(range(len(cores))) self.ax.set_xticklabels([f'Core {core}' for core in cores]) self.ax.get_yaxis().set_visible(False) self.figure.colorbar(self.heatmap_img, ax=self.ax) # 只绘制变更部分(性能优化) self.canvas.draw_idle() def __del__(self): """清理资源""" self.interrupt_monitor_running = False if __name__ == "__main__": root = tk.Tk() app = TimerApp(root) try: root.mainloop() finally: # 确保线程安全退出 app.interrupt_monitor_running = False 这个代码里面有几个问题。时间分解分析及多喝利用率热力图没有任何实际作用需要删除。并且需要新增时间的统计信息做成图替换原来的热力图。当我选择一个计时方案时,应该判断另一个计时器是否处于开始状态,如果处于开始状态则应该正常显示而不是显示--。需要新增多次计时的功能,可能需要看每次的时间。给我完整的代码
最新发布
10-14
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值