return ncpu;
}
+static int __get_cpu_stat(FILE *fp, struct cpu_stat *data)
+{
+ int ret;
+
+ if (!fp || !data)
+ return -EINVAL;
+
+ ret = fscanf(fp, "%s %"PRId64" %"PRId64" %"PRId64" %"PRId64" \
+ %"PRId64" %"PRId64" %"PRId64" %*s %*s %*s",
+ data->name,
+ &data->user,
+ &data->system,
+ &data->nice,
+ &data->idle,
+ &data->wait,
+ &data->hard_irq,
+ &data->soft_irq);
+ if (ret < 8) {
+ ret = -EINVAL;
+ goto err;
+ }
+
+ if (strncmp(data->name, "cpu", 3)) {
+ /* In case of not 'cpu*' string */
+ ret = -EINVAL;
+ goto err;
+ } else if (strlen(data->name) > 3) {
+ /* In case of 'cpu%u' string */
+ ret = sscanf(data->name, "cpu%4u", &data->cpu);
+ if (ret < 1) {
+ ret = -EINVAL;
+ goto err;
+ }
+ } else {
+ /* In case of 'cpu' string */
+ data->cpu = -1;
+ }
+
+ return 0;
+err:
+ memset(data, 0, sizeof(struct cpu_stat));
+ return ret;
+}
+
int kernel_get_online_cpu_num(void)
{
return __get_cpu_num("/sys/devices/system/cpu/online");
{
return __get_cpu_num("/sys/devices/system/cpu/possible");
}
+
+int kernel_get_total_cpu_stat(struct cpu_stat *total)
+{
+ FILE *fp;
+ int ret;
+
+ if (!total)
+ return -EINVAL;
+
+ fp = fopen("/proc/stat", "r");
+ if (!fp)
+ return -EINVAL;
+
+ ret = __get_cpu_stat(fp, total);
+ if (ret < 0)
+ ret = -EINVAL;
+ fclose(fp);
+
+ return ret;
+}
+
+int kernel_get_per_cpu_stat(struct cpu_stat *cpus, int num_possible_cpus,
+ int *num_online_cpus)
+{
+ FILE *fp;
+ char buf[BUFF_MAX];
+ int i, ret = 0, count = 0;
+
+ if (!cpus)
+ return -EINVAL;
+
+ *num_online_cpus = 0;
+
+ fp = fopen("/proc/stat", "r");
+ if (!fp)
+ return -EINVAL;
+
+ /* Skip the first line of total cpu utilizaiton. */
+ if (!fgets(buf, BUFF_MAX, fp)) {
+ ret = -EINVAL;
+ goto err;
+ }
+
+ /* Get per-cpu utilization data */
+ for (i = 0; i < num_possible_cpus; i++) {
+ ret = __get_cpu_stat(fp, &cpus[i]);
+ if (ret < 0)
+ break;
+ count++;
+ }
+ *num_online_cpus = count;
+
+err:
+ fclose(fp);
+
+ return ret;
+}