OLD | NEW |
1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
| 5 // Implementation of bootstat_log(), part of the Chromium OS 'bootstat' |
| 6 // facility. |
| 7 |
| 8 #include "bootstat.h" |
| 9 |
5 #include <stdio.h> | 10 #include <stdio.h> |
6 #include <stddef.h> | 11 #include <stddef.h> |
7 | 12 |
8 #include <sys/types.h> | 13 #include <sys/types.h> |
9 #include <sys/stat.h> | 14 #include <sys/stat.h> |
10 #include <sys/fcntl.h> | 15 #include <sys/fcntl.h> |
11 #include <unistd.h> | 16 #include <unistd.h> |
12 | 17 |
13 #include "bootstat.h" | |
14 | |
15 // | 18 // |
16 // Paths to the statistics files we snapshot as part of the data to | 19 // Paths to the statistics files we snapshot as part of the data to |
17 // be logged. | 20 // be logged. |
18 // | 21 // |
19 #define UPTIME "/proc/uptime" | 22 static const char kUptimeStatisticsFileName[] = "/proc/uptime"; |
20 | 23 |
21 #if defined (__amd64__) || defined (__x86_64__) | 24 #if defined (__amd64__) || defined (__x86_64__) || defined (__i386__) |
22 #define ROOTDEV "sda" | 25 static const char kDiskStatisticsFileName[] = "/sys/block/sda/stat"; |
| 26 #elif defined (__arm__) |
| 27 static const char kDiskStatisticsFileName[] = "/sys/block/mmcblk0/stat"; |
| 28 #else |
| 29 #error "unknown processor type?" |
23 #endif | 30 #endif |
24 | 31 |
25 #if defined (__arm__) | |
26 #define ROOTDEV "mmcblk0" | |
27 #endif | |
28 | |
29 #ifdef ROOTDEV | |
30 #define ROOTSTAT "/sys/block/" ROOTDEV "/stat" | |
31 #endif | |
32 | |
33 // | |
34 // Length of the longest valid string naming an event, not counting | |
35 // the terminating NUL character. Arbitrarily chosen, but intended | |
36 // to be somewhat shorter than any valid file or path name. | |
37 // | |
38 #define MAX_EVENT_LEN 63 | |
39 | 32 |
40 // | 33 // |
41 // Maximum length of any pathname for storing event statistics. | 34 // Maximum length of any pathname for storing event statistics. |
42 // Also arbitrarily chosen, but see the comment below about | 35 // Arbitrarily chosen, but see the comment below about truncation. |
43 // truncation. | |
44 // | 36 // |
45 #define MAX_STAT_PATH 128 | 37 #define MAX_STAT_PATH 128 |
46 | 38 |
47 // | 39 // |
48 // Output file creation mode: 0666, a.k.a. rw-rw-rw-. | 40 // Output file creation mode: 0666, a.k.a. rw-rw-rw-. |
49 // | 41 // |
50 #define OFMODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) | 42 static const int kFileCreationMode = |
| 43 (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); |
51 | 44 |
52 static void append_logdata(const char *ifname, | 45 |
53 const char *ofprefix, const char *event_id) | 46 static void append_logdata(const char* input_path, |
| 47 const char* output_name_prefix, |
| 48 const char* event_name) |
54 { | 49 { |
55 char ofpath[MAX_STAT_PATH]; | 50 char output_path[MAX_STAT_PATH]; |
56 char buffer[256]; | 51 char buffer[256]; |
57 ssize_t nbytes; | 52 ssize_t num_read; |
58 int ifd, ofd; | 53 int ifd, ofd; |
59 | 54 |
60 ifd = open(ifname, O_RDONLY); | 55 ifd = open(input_path, O_RDONLY); |
61 if (ifd < 0) { | 56 if (ifd < 0) { |
62 return; | 57 return; |
63 } | 58 } |
64 | 59 |
65 // | 60 // |
66 // We don't want the file name "/tmp/uptime-..." truncated | 61 // We don't want the file name "/tmp/uptime-..." truncated |
67 // differently from the the name "/tmp/disk-...", so we truncate | 62 // differently from the the name "/tmp/disk-...", so we truncate |
68 // event_id separately using the "%.*s" format. | 63 // event_name separately using the "%.*s" format. |
69 // | 64 // |
70 // We expect that MAX_STAT_LEN is enough smaller than | 65 // We expect that BOOTSTAT_MAX_EVENT_LEN is enough smaller than |
71 // MAX_STAT_PATH that ofpath will never be truncated. However, | 66 // MAX_STAT_PATH that output_path will never be truncated. |
72 // to be safe, we also stuff our own terminating '\0', since | |
73 // snprintf() won't put it there in the case of truncation. | |
74 // | 67 // |
75 (void) snprintf(ofpath, sizeof (ofpath) - 1, "/tmp/%s-%.*s", | 68 (void) snprintf(output_path, sizeof(output_path), "/tmp/%s-%.*s", |
76 ofprefix, MAX_EVENT_LEN, event_id); | 69 output_name_prefix, |
77 ofpath[sizeof (ofpath) - 1] = '\0'; | 70 BOOTSTAT_MAX_EVENT_LEN - 1, event_name); |
78 ofd = open(ofpath, O_WRONLY | O_APPEND | O_CREAT, OFMODE); | 71 ofd = open(output_path, O_WRONLY | O_APPEND | O_CREAT, |
| 72 kFileCreationMode); |
79 if (ofd < 0) { | 73 if (ofd < 0) { |
80 (void) close(ifd); | 74 (void) close(ifd); |
81 return; | 75 return; |
82 } | 76 } |
83 | 77 |
84 while ((nbytes = read(ifd, buffer, sizeof (buffer))) > 0) { | 78 while ((num_read = read(ifd, buffer, sizeof(buffer))) > 0) { |
85 ssize_t wnbytes = write(ofd, buffer, nbytes); | 79 ssize_t num_written = write(ofd, buffer, num_read); |
86 if (wnbytes != nbytes) | 80 if (num_written != num_read) |
87 break; | 81 break; |
88 } | 82 } |
89 (void) close(ofd); | 83 (void) close(ofd); |
90 (void) close(ifd); | 84 (void) close(ifd); |
91 } | 85 } |
92 | 86 |
93 | 87 |
94 void bootstat_log(const char *event_id) | 88 void bootstat_log(const char* event_name) |
95 { | 89 { |
96 append_logdata(UPTIME, "uptime", event_id); | 90 append_logdata(kUptimeStatisticsFileName, "uptime", event_name); |
97 #ifdef ROOTSTAT | 91 append_logdata(kDiskStatisticsFileName, "disk", event_name); |
98 append_logdata(ROOTSTAT, "disk", event_id); | |
99 #endif | |
100 } | 92 } |
OLD | NEW |