* fs/procfs/fs_procfscpuload.c
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <time.h>
#include <string.h>
#include <fcntl.h>
#include <assert.h>
#include <errno.h>
#include <debug.h>
#include <nuttx/clock.h>
#include <nuttx/kmalloc.h>
#include <nuttx/fs/fs.h>
#include <nuttx/fs/procfs.h>
#include "fs_heap.h"
#if !defined(CONFIG_DISABLE_MOUNTPOINT) && defined(CONFIG_FS_PROCFS)
#if !defined(CONFIG_SCHED_CPULOAD_NONE) && \
!defined(CONFIG_FS_PROCFS_EXCLUDE_CPULOAD)
* Pre-processor Definitions
****************************************************************************/
* to handle the longest line generated by this logic.
*/
#define CPULOAD_LINELEN 16
* Private Types
****************************************************************************/
struct cpuload_file_s
{
struct procfs_file_s base;
unsigned int linesize;
char line[CPULOAD_LINELEN];
};
* Private Function Prototypes
****************************************************************************/
static int cpuload_open(FAR struct file *filep, FAR const char *relpath,
int oflags, mode_t mode);
static int cpuload_close(FAR struct file *filep);
static ssize_t cpuload_read(FAR struct file *filep, FAR char *buffer,
size_t buflen);
* Public Data
****************************************************************************/
* We use the old-fashioned kind of initializers so that this will compile
* with any compiler.
*/
const struct procfs_operations g_cpuload_operations =
{
cpuload_open,
cpuload_close,
cpuload_read,
};
* Private Functions
****************************************************************************/
* Name: cpuload_open
****************************************************************************/
static int cpuload_open(FAR struct file *filep, FAR const char *relpath,
int oflags, mode_t mode)
{
FAR struct cpuload_file_s *attr;
finfo("Open '%s'\n", relpath);
* access is not permitted.
*
* REVISIT: Write-able proc files could be quite useful.
*/
if ((oflags & O_WRONLY) != 0 || (oflags & O_RDONLY) == 0)
{
ferr("ERROR: Only O_RDONLY supported\n");
return -EACCES;
}
attr = fs_heap_zalloc(sizeof(struct cpuload_file_s));
if (!attr)
{
ferr("ERROR: Failed to allocate file attributes\n");
return -ENOMEM;
}
filep->f_priv = (FAR void *)attr;
return OK;
}
* Name: cpuload_close
****************************************************************************/
static int cpuload_close(FAR struct file *filep)
{
FAR struct cpuload_file_s *attr;
attr = (FAR struct cpuload_file_s *)filep->f_priv;
DEBUGASSERT(attr);
fs_heap_free(attr);
filep->f_priv = NULL;
return OK;
}
* Name: cpuload_read
****************************************************************************/
static ssize_t cpuload_read(FAR struct file *filep, FAR char *buffer,
size_t buflen)
{
FAR struct cpuload_file_s *attr;
size_t linesize;
off_t offset;
ssize_t ret;
finfo("buffer=%p buflen=%d\n", buffer, (int)buflen);
attr = (FAR struct cpuload_file_s *)filep->f_priv;
DEBUGASSERT(attr);
* the cached system time from the previous read(). It is necessary
* save the cached value in case, for example, the user is reading
* the time one byte at a time. In that case, the time must remain
* stable throughout the reads.
*/
if (filep->f_pos == 0)
{
clock_t total = 0;
clock_t active = 0;
uint32_t intpart;
uint32_t fracpart;
* fail if the PID is not valid. This, however, should never happen
* for the IDLE thread.
*/
#ifdef CONFIG_SMP
struct cpuload_s cpuloads[CONFIG_SMP_NCPUS];
uint32_t i;
for (i = 0; i < CONFIG_SMP_NCPUS; i++)
{
DEBUGVERIFY(clock_cpuload(i, &cpuloads[i]));
active += cpuloads[i].active;
}
total = cpuloads[0].total;
#else
struct cpuload_s cpuload;
DEBUGVERIFY(clock_cpuload(0, &cpuload));
active = cpuload.active;
total = cpuload.total;
#endif
if (active > total)
{
active = total;
}
* on real hardware.
*/
if (total > 0)
{
uint32_t tmp;
tmp = 1000 - (1000 * active) / total;
intpart = tmp / 10;
fracpart = tmp - 10 * intpart;
}
else
{
intpart = 0;
fracpart = 0;
}
linesize = procfs_snprintf(attr->line, CPULOAD_LINELEN,
"%3" PRId32 ".%01" PRId32 "%%\n",
intpart, fracpart);
attr->linesize = linesize;
}
offset = filep->f_pos;
ret = procfs_memcpy(attr->line, attr->linesize, buffer, buflen, &offset);
if (ret > 0)
{
filep->f_pos += ret;
}
return ret;
}
#endif
#endif