1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @brief Block Device Statistics Command |
---|
5 | * @ingroup rtems_blkdev Block Device Management |
---|
6 | */ |
---|
7 | |
---|
8 | /* |
---|
9 | * Copyright (c) 2012 embedded brains GmbH. All rights reserved. |
---|
10 | * |
---|
11 | * embedded brains GmbH |
---|
12 | * Obere Lagerstr. 30 |
---|
13 | * 82178 Puchheim |
---|
14 | * Germany |
---|
15 | * <rtems@embedded-brains.de> |
---|
16 | * |
---|
17 | * The license and distribution terms for this file may be |
---|
18 | * found in the file LICENSE in this distribution or at |
---|
19 | * http://www.rtems.com/license/LICENSE. |
---|
20 | */ |
---|
21 | |
---|
22 | #if HAVE_CONFIG_H |
---|
23 | #include "config.h" |
---|
24 | #endif |
---|
25 | |
---|
26 | #include <rtems/blkdev.h> |
---|
27 | |
---|
28 | #include <sys/stat.h> |
---|
29 | #include <fcntl.h> |
---|
30 | #include <unistd.h> |
---|
31 | #include <errno.h> |
---|
32 | #include <string.h> |
---|
33 | |
---|
34 | void rtems_blkstats(FILE *output, const char *device, bool reset) |
---|
35 | { |
---|
36 | int fd = open(device, O_RDONLY); |
---|
37 | |
---|
38 | if (fd >= 0) { |
---|
39 | struct stat st; |
---|
40 | int rv; |
---|
41 | |
---|
42 | rv = fstat(fd, &st); |
---|
43 | if (rv == 0) { |
---|
44 | if (S_ISBLK(st.st_mode)) { |
---|
45 | if (reset) { |
---|
46 | rv = rtems_disk_fd_reset_device_stats(fd); |
---|
47 | if (rv != 0) { |
---|
48 | fprintf(output, "error: reset stats: %s\n", strerror(errno)); |
---|
49 | } |
---|
50 | } else { |
---|
51 | rtems_blkdev_stats stats; |
---|
52 | |
---|
53 | rv = rtems_disk_fd_get_device_stats(fd, &stats); |
---|
54 | if (rv == 0) { |
---|
55 | rtems_blkdev_print_stats( |
---|
56 | &stats, |
---|
57 | (rtems_printk_plugin_t) fprintf, |
---|
58 | output |
---|
59 | ); |
---|
60 | } else { |
---|
61 | fprintf(output, "error: get stats: %s\n", strerror(errno)); |
---|
62 | } |
---|
63 | } |
---|
64 | } else { |
---|
65 | fprintf(output, "error: not a block device\n"); |
---|
66 | } |
---|
67 | } else { |
---|
68 | fprintf(output, "error: get file stats: %s\n", strerror(errno)); |
---|
69 | } |
---|
70 | |
---|
71 | rv = close(fd); |
---|
72 | if (rv != 0) { |
---|
73 | fprintf(output, "error: close device: %s\n", strerror(errno)); |
---|
74 | } |
---|
75 | } else { |
---|
76 | fprintf(output, "error: open device: %s\n", strerror(errno)); |
---|
77 | } |
---|
78 | } |
---|