fs_procfscpuload.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /****************************************************************************
  2. * fs/procfs/fs_procfscpuload.c
  3. *
  4. * Copyright (C) 2014, 2017 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Included Files
  37. ****************************************************************************/
  38. #include <nuttx/config.h>
  39. #include <sys/types.h>
  40. #include <sys/statfs.h>
  41. #include <sys/stat.h>
  42. #include <stdint.h>
  43. #include <stdbool.h>
  44. #include <stdio.h>
  45. #include <stdlib.h>
  46. #include <time.h>
  47. #include <string.h>
  48. #include <fcntl.h>
  49. #include <assert.h>
  50. #include <errno.h>
  51. #include <debug.h>
  52. #include <nuttx/clock.h>
  53. #include <nuttx/kmalloc.h>
  54. #include <nuttx/fs/fs.h>
  55. #include <nuttx/fs/procfs.h>
  56. #if !defined(CONFIG_DISABLE_MOUNTPOINT) && defined(CONFIG_FS_PROCFS)
  57. #if defined(CONFIG_SCHED_CPULOAD) && !defined(CONFIG_FS_PROCFS_EXCLUDE_CPULOAD)
  58. /****************************************************************************
  59. * Pre-processor Definitions
  60. ****************************************************************************/
  61. /* Determines the size of an intermediate buffer that must be large enough
  62. * to handle the longest line generated by this logic.
  63. */
  64. #define CPULOAD_LINELEN 16
  65. /****************************************************************************
  66. * Private Types
  67. ****************************************************************************/
  68. /* This structure describes one open "file" */
  69. struct cpuload_file_s
  70. {
  71. struct procfs_file_s base; /* Base open file structure */
  72. unsigned int linesize; /* Number of valid characters in line[] */
  73. char line[CPULOAD_LINELEN]; /* Pre-allocated buffer for formatted lines */
  74. };
  75. /****************************************************************************
  76. * Private Function Prototypes
  77. ****************************************************************************/
  78. /* File system methods */
  79. static int cpuload_open(FAR struct file *filep, FAR const char *relpath,
  80. int oflags, mode_t mode);
  81. static int cpuload_close(FAR struct file *filep);
  82. static ssize_t cpuload_read(FAR struct file *filep, FAR char *buffer,
  83. size_t buflen);
  84. static int cpuload_dup(FAR const struct file *oldp,
  85. FAR struct file *newp);
  86. static int cpuload_stat(FAR const char *relpath, FAR struct stat *buf);
  87. /****************************************************************************
  88. * Public Data
  89. ****************************************************************************/
  90. /* See fs_mount.c -- this structure is explicitly externed there.
  91. * We use the old-fashioned kind of initializers so that this will compile
  92. * with any compiler.
  93. */
  94. const struct procfs_operations cpuload_operations =
  95. {
  96. cpuload_open, /* open */
  97. cpuload_close, /* close */
  98. cpuload_read, /* read */
  99. NULL, /* write */
  100. cpuload_dup, /* dup */
  101. NULL, /* opendir */
  102. NULL, /* closedir */
  103. NULL, /* readdir */
  104. NULL, /* rewinddir */
  105. cpuload_stat /* stat */
  106. };
  107. /****************************************************************************
  108. * Private Functions
  109. ****************************************************************************/
  110. /****************************************************************************
  111. * Name: cpuload_open
  112. ****************************************************************************/
  113. static int cpuload_open(FAR struct file *filep, FAR const char *relpath,
  114. int oflags, mode_t mode)
  115. {
  116. FAR struct cpuload_file_s *attr;
  117. finfo("Open '%s'\n", relpath);
  118. /* PROCFS is read-only. Any attempt to open with any kind of write
  119. * access is not permitted.
  120. *
  121. * REVISIT: Write-able proc files could be quite useful.
  122. */
  123. if ((oflags & O_WRONLY) != 0 || (oflags & O_RDONLY) == 0)
  124. {
  125. ferr("ERROR: Only O_RDONLY supported\n");
  126. return -EACCES;
  127. }
  128. /* "cpuload" is the only acceptable value for the relpath */
  129. if (strcmp(relpath, "cpuload") != 0)
  130. {
  131. ferr("ERROR: relpath is '%s'\n", relpath);
  132. return -ENOENT;
  133. }
  134. /* Allocate a container to hold the file attributes */
  135. attr = (FAR struct cpuload_file_s *)kmm_zalloc(sizeof(struct cpuload_file_s));
  136. if (!attr)
  137. {
  138. ferr("ERROR: Failed to allocate file attributes\n");
  139. return -ENOMEM;
  140. }
  141. /* Save the attributes as the open-specific state in filep->f_priv */
  142. filep->f_priv = (FAR void *)attr;
  143. return OK;
  144. }
  145. /****************************************************************************
  146. * Name: cpuload_close
  147. ****************************************************************************/
  148. static int cpuload_close(FAR struct file *filep)
  149. {
  150. FAR struct cpuload_file_s *attr;
  151. /* Recover our private data from the struct file instance */
  152. attr = (FAR struct cpuload_file_s *)filep->f_priv;
  153. DEBUGASSERT(attr);
  154. /* Release the file attributes structure */
  155. kmm_free(attr);
  156. filep->f_priv = NULL;
  157. return OK;
  158. }
  159. /****************************************************************************
  160. * Name: cpuload_read
  161. ****************************************************************************/
  162. static ssize_t cpuload_read(FAR struct file *filep, FAR char *buffer,
  163. size_t buflen)
  164. {
  165. FAR struct cpuload_file_s *attr;
  166. size_t linesize;
  167. off_t offset;
  168. ssize_t ret;
  169. finfo("buffer=%p buflen=%d\n", buffer, (int)buflen);
  170. /* Recover our private data from the struct file instance */
  171. attr = (FAR struct cpuload_file_s *)filep->f_priv;
  172. DEBUGASSERT(attr);
  173. /* If f_pos is zero, then sample the system time. Otherwise, use
  174. * the cached system time from the previous read(). It is necessary
  175. * save the cached value in case, for example, the user is reading
  176. * the time one byte at a time. In that case, the time must remain
  177. * stable throughout the reads.
  178. */
  179. if (filep->f_pos == 0)
  180. {
  181. struct cpuload_s cpuload;
  182. uint32_t intpart;
  183. uint32_t fracpart;
  184. /* Sample the counts for the IDLE thread. clock_cpuload should only
  185. * fail if the PID is not valid. This, however, should never happen
  186. * for the IDLE thread.
  187. */
  188. DEBUGVERIFY(clock_cpuload(0, &cpuload));
  189. /* On the simulator, you may hit cpuload.total == 0, but probably never on
  190. * real hardware.
  191. */
  192. if (cpuload.total > 0)
  193. {
  194. uint32_t tmp;
  195. tmp = 1000 - (1000 * cpuload.active) / cpuload.total;
  196. intpart = tmp / 10;
  197. fracpart = tmp - 10 * intpart;
  198. }
  199. else
  200. {
  201. intpart = 0;
  202. fracpart = 0;
  203. }
  204. linesize = snprintf(attr->line, CPULOAD_LINELEN, "%3d.%01d%%",
  205. intpart, fracpart);
  206. /* Save the linesize in case we are re-entered with f_pos > 0 */
  207. attr->linesize = linesize;
  208. }
  209. /* Transfer the system up time to user receive buffer */
  210. offset = filep->f_pos;
  211. ret = procfs_memcpy(attr->line, attr->linesize, buffer, buflen, &offset);
  212. /* Update the file offset */
  213. if (ret > 0)
  214. {
  215. filep->f_pos += ret;
  216. }
  217. return ret;
  218. }
  219. /****************************************************************************
  220. * Name: cpuload_dup
  221. *
  222. * Description:
  223. * Duplicate open file data in the new file structure.
  224. *
  225. ****************************************************************************/
  226. static int cpuload_dup(FAR const struct file *oldp, FAR struct file *newp)
  227. {
  228. FAR struct cpuload_file_s *oldattr;
  229. FAR struct cpuload_file_s *newattr;
  230. finfo("Dup %p->%p\n", oldp, newp);
  231. /* Recover our private data from the old struct file instance */
  232. oldattr = (FAR struct cpuload_file_s *)oldp->f_priv;
  233. DEBUGASSERT(oldattr);
  234. /* Allocate a new container to hold the task and attribute selection */
  235. newattr = (FAR struct cpuload_file_s *)kmm_malloc(sizeof(struct cpuload_file_s));
  236. if (!newattr)
  237. {
  238. ferr("ERROR: Failed to allocate file attributes\n");
  239. return -ENOMEM;
  240. }
  241. /* The copy the file attributes from the old attributes to the new */
  242. memcpy(newattr, oldattr, sizeof(struct cpuload_file_s));
  243. /* Save the new attributes in the new file structure */
  244. newp->f_priv = (FAR void *)newattr;
  245. return OK;
  246. }
  247. /****************************************************************************
  248. * Name: cpuload_stat
  249. *
  250. * Description: Return information about a file or directory
  251. *
  252. ****************************************************************************/
  253. static int cpuload_stat(const char *relpath, struct stat *buf)
  254. {
  255. /* "cpuload" is the only acceptable value for the relpath */
  256. if (strcmp(relpath, "cpuload") != 0)
  257. {
  258. ferr("ERROR: relpath is '%s'\n", relpath);
  259. return -ENOENT;
  260. }
  261. /* "cpuload" is the name for a read-only file */
  262. memset(buf, 0, sizeof(struct stat));
  263. buf->st_mode = S_IFREG | S_IROTH | S_IRGRP | S_IRUSR;
  264. return OK;
  265. }
  266. /****************************************************************************
  267. * Public Functions
  268. ****************************************************************************/
  269. #endif /* CONFIG_FS_PROCFS_EXCLUDE_CPULOAD */
  270. #endif /* !CONFIG_DISABLE_MOUNTPOINT && CONFIG_FS_PROCFS */