Chromium Code Reviews| 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' | 5 // Implementation of bootstat_log(), part of the Chromium OS 'bootstat' |
| 6 // facility. | 6 // facility. |
| 7 | 7 |
| 8 #include "bootstat.h" | 8 #include "bootstat.h" |
| 9 #include "bootstat_test.h" | |
| 9 | 10 |
| 10 #include <stdio.h> | 11 #include <stdio.h> |
| 11 #include <stddef.h> | 12 #include <stddef.h> |
| 12 | 13 |
| 14 #include <sys/fcntl.h> | |
| 15 #include <sys/param.h> | |
| 16 #include <sys/stat.h> | |
| 13 #include <sys/types.h> | 17 #include <sys/types.h> |
| 14 #include <sys/stat.h> | |
| 15 #include <sys/fcntl.h> | |
| 16 #include <unistd.h> | 18 #include <unistd.h> |
| 17 | 19 |
| 18 // | 20 // |
| 21 // Default path to directory where output statistics will be stored. | |
| 22 // | |
| 23 static const char kDefaultOutputDirectoryName[] = "/tmp"; | |
| 24 | |
| 25 // | |
| 19 // Paths to the statistics files we snapshot as part of the data to | 26 // Paths to the statistics files we snapshot as part of the data to |
| 20 // be logged. | 27 // be logged. |
| 21 // | 28 // |
| 22 static const char kUptimeStatisticsFileName[] = "/proc/uptime"; | 29 static const char kDefaultUptimeStatisticsFileName[] = "/proc/uptime"; |
| 23 | 30 |
| 24 #if defined (__amd64__) || defined (__x86_64__) || defined (__i386__) | 31 #if defined (__amd64__) || defined (__x86_64__) || defined (__i386__) |
| 25 static const char kDiskStatisticsFileName[] = "/sys/block/sda/stat"; | 32 static const char kDefaultDiskStatisticsFileName[] = "/sys/block/sda/stat"; |
| 26 #elif defined (__arm__) | 33 #elif defined (__arm__) |
| 27 static const char kDiskStatisticsFileName[] = "/sys/block/mmcblk0/stat"; | 34 static const char kDefaultDiskStatisticsFileName[] = "/sys/block/mmcblk0/stat"; |
| 28 #else | 35 #else |
| 29 #error "unknown processor type?" | 36 #error "unknown processor type?" |
| 30 #endif | 37 #endif |
| 31 | 38 |
| 32 | 39 static const char *output_directory_name = kDefaultOutputDirectoryName; |
| 33 // | 40 static const char *uptime_statistics_file_name = |
| 34 // Maximum length of any pathname for storing event statistics. | 41 kDefaultUptimeStatisticsFileName; |
| 35 // Arbitrarily chosen, but see the comment below about truncation. | 42 static const char *disk_statistics_file_name = kDefaultDiskStatisticsFileName; |
| 36 // | |
| 37 #define MAX_STAT_PATH 128 | |
| 38 | |
| 39 // | |
| 40 // Output file creation mode: 0666, a.k.a. rw-rw-rw-. | |
| 41 // | |
| 42 static const int kFileCreationMode = | |
| 43 (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); | |
| 44 | |
| 45 | 43 |
| 46 static void append_logdata(const char* input_path, | 44 static void append_logdata(const char* input_path, |
| 47 const char* output_name_prefix, | 45 const char* output_name_prefix, |
| 48 const char* event_name) | 46 const char* event_name) |
| 49 { | 47 { |
| 50 char output_path[MAX_STAT_PATH]; | 48 const mode_t kFileCreationMode = |
| 49 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; | |
| 50 char output_path[PATH_MAX]; | |
| 51 char buffer[256]; | 51 char buffer[256]; |
| 52 ssize_t num_read; | 52 ssize_t num_read; |
| 53 int ifd, ofd; | 53 int ifd, ofd; |
| 54 | 54 |
| 55 ifd = open(input_path, O_RDONLY); | 55 ifd = open(input_path, O_RDONLY); |
| 56 if (ifd < 0) { | 56 if (ifd < 0) { |
| 57 return; | 57 return; |
| 58 } | 58 } |
| 59 | 59 |
| 60 // | 60 // |
| 61 // We don't want the file name "/tmp/uptime-..." truncated | 61 // We don't want the file name ".../uptime-..." truncated |
| 62 // differently from the the name "/tmp/disk-...", so we truncate | 62 // differently from the the name ".../disk-...", so we truncate |
| 63 // event_name separately using the "%.*s" format. | 63 // event_name separately using the "%.*s" format. |
|
kmixter1
2010/12/06 04:23:52
Confused by this comment. Say BOOTSTAT_MAX_EVENT_
| |
| 64 // | 64 // |
| 65 // We expect that BOOTSTAT_MAX_EVENT_LEN is enough smaller than | 65 (void) snprintf(output_path, sizeof(output_path), "%s/%s-%.*s", |
|
kmixter1
2010/12/06 04:23:52
space after (void) seems unusual. Actually I gues
| |
| 66 // MAX_STAT_PATH that output_path will never be truncated. | 66 output_directory_name, |
| 67 // | |
| 68 (void) snprintf(output_path, sizeof(output_path), "/tmp/%s-%.*s", | |
| 69 output_name_prefix, | 67 output_name_prefix, |
| 70 BOOTSTAT_MAX_EVENT_LEN - 1, event_name); | 68 BOOTSTAT_MAX_EVENT_LEN - 1, event_name); |
| 71 ofd = open(output_path, O_WRONLY | O_APPEND | O_CREAT, | 69 ofd = open(output_path, O_WRONLY | O_APPEND | O_CREAT, |
| 72 kFileCreationMode); | 70 kFileCreationMode); |
| 73 if (ofd < 0) { | 71 if (ofd < 0) { |
| 74 (void) close(ifd); | 72 (void) close(ifd); |
| 75 return; | 73 return; |
| 76 } | 74 } |
| 77 | 75 |
| 78 while ((num_read = read(ifd, buffer, sizeof(buffer))) > 0) { | 76 while ((num_read = read(ifd, buffer, sizeof(buffer))) > 0) { |
| 79 ssize_t num_written = write(ofd, buffer, num_read); | 77 ssize_t num_written = write(ofd, buffer, num_read); |
| 80 if (num_written != num_read) | 78 if (num_written != num_read) |
| 81 break; | 79 break; |
| 82 } | 80 } |
| 83 (void) close(ofd); | 81 (void) close(ofd); |
| 84 (void) close(ifd); | 82 (void) close(ifd); |
| 85 } | 83 } |
| 86 | 84 |
| 87 | 85 |
| 88 void bootstat_log(const char* event_name) | 86 void bootstat_log(const char* event_name) |
| 89 { | 87 { |
| 90 append_logdata(kUptimeStatisticsFileName, "uptime", event_name); | 88 append_logdata(uptime_statistics_file_name, "uptime", event_name); |
| 91 append_logdata(kDiskStatisticsFileName, "disk", event_name); | 89 append_logdata(disk_statistics_file_name, "disk", event_name); |
| 92 } | 90 } |
| 91 | |
| 92 | |
| 93 #ifdef CONFIG_UNIT_TEST | |
| 94 void bootstat_set_output_directory(const char* dirname) | |
| 95 { | |
| 96 if (dirname != NULL) | |
| 97 output_directory_name = dirname; | |
| 98 else | |
| 99 output_directory_name = kDefaultOutputDirectoryName; | |
| 100 } | |
| 101 | |
| 102 | |
| 103 void bootstat_set_uptime_file_name(const char* filename) | |
| 104 { | |
| 105 if (filename != NULL) | |
| 106 uptime_statistics_file_name = filename; | |
| 107 else | |
| 108 uptime_statistics_file_name = kDefaultUptimeStatisticsFileName; | |
| 109 } | |
| 110 | |
| 111 | |
| 112 void bootstat_set_disk_file_name(const char* filename) | |
| 113 { | |
| 114 if (filename != NULL) | |
| 115 disk_statistics_file_name = filename; | |
| 116 else | |
| 117 disk_statistics_file_name = kDefaultDiskStatisticsFileName; | |
| 118 } | |
| 119 #endif | |
| OLD | NEW |