0001
0002
0003
0004
0005
0006
0007 #include <unistd.h>
0008 #include <stdlib.h>
0009 #include <stdio.h>
0010 #include <fcntl.h>
0011 #include <errno.h>
0012 #include <time.h>
0013 #include <string.h>
0014 #include <signal.h>
0015 #include <sys/ioctl.h>
0016 #include <linux/hdreg.h>
0017
0018 #ifdef DEBUG
0019 #define D(x) x
0020 #else
0021 #define D(x)
0022 #endif
0023
0024 int endit = 0;
0025
0026
0027
0028
0029 static int check_powermode(int fd)
0030 {
0031 unsigned char args[4] = {WIN_CHECKPOWERMODE1,0,0,0};
0032 int state;
0033
0034 if (ioctl(fd, HDIO_DRIVE_CMD, &args)
0035 && (args[0] = WIN_CHECKPOWERMODE2)
0036 && ioctl(fd, HDIO_DRIVE_CMD, &args)) {
0037 if (errno != EIO || args[0] != 0 || args[1] != 0) {
0038 state = -1;
0039 } else
0040 state = 0;
0041 } else {
0042 state = (args[2] == 255) ? 1 : 0;
0043 }
0044 D(printf(" drive state is: %d\n", state));
0045
0046 return state;
0047 }
0048
0049 static char *state_name(int i)
0050 {
0051 if (i == -1) return "unknown";
0052 if (i == 0) return "sleeping";
0053 if (i == 1) return "active";
0054
0055 return "internal error";
0056 }
0057
0058 static char *myctime(time_t time)
0059 {
0060 char *ts = ctime(&time);
0061 ts[strlen(ts) - 1] = 0;
0062
0063 return ts;
0064 }
0065
0066 static void measure(int fd)
0067 {
0068 time_t start_time;
0069 int last_state;
0070 time_t last_time;
0071 int curr_state;
0072 time_t curr_time = 0;
0073 time_t time_diff;
0074 time_t active_time = 0;
0075 time_t sleep_time = 0;
0076 time_t unknown_time = 0;
0077 time_t total_time = 0;
0078 int changes = 0;
0079 float tmp;
0080
0081 printf("Starting measurements\n");
0082
0083 last_state = check_powermode(fd);
0084 start_time = last_time = time(0);
0085 printf(" System is in state %s\n\n", state_name(last_state));
0086
0087 while(!endit) {
0088 sleep(1);
0089 curr_state = check_powermode(fd);
0090
0091 if (curr_state != last_state || endit) {
0092 changes++;
0093 curr_time = time(0);
0094 time_diff = curr_time - last_time;
0095
0096 if (last_state == 1) active_time += time_diff;
0097 else if (last_state == 0) sleep_time += time_diff;
0098 else unknown_time += time_diff;
0099
0100 last_state = curr_state;
0101 last_time = curr_time;
0102
0103 printf("%s: State-change to %s\n", myctime(curr_time),
0104 state_name(curr_state));
0105 }
0106 }
0107 changes--;
0108
0109 total_time = time(0) - start_time;
0110 printf("\nTotal running time: %lus\n", curr_time - start_time);
0111 printf(" State changed %d times\n", changes);
0112
0113 tmp = (float)sleep_time / (float)total_time * 100;
0114 printf(" Time in sleep state: %lus (%.2f%%)\n", sleep_time, tmp);
0115 tmp = (float)active_time / (float)total_time * 100;
0116 printf(" Time in active state: %lus (%.2f%%)\n", active_time, tmp);
0117 tmp = (float)unknown_time / (float)total_time * 100;
0118 printf(" Time in unknown state: %lus (%.2f%%)\n", unknown_time, tmp);
0119 }
0120
0121 static void ender(int s)
0122 {
0123 endit = 1;
0124 }
0125
0126 static void usage(void)
0127 {
0128 puts("usage: dslm [-w <time>] <disk>");
0129 exit(0);
0130 }
0131
0132 int main(int argc, char **argv)
0133 {
0134 int fd;
0135 char *disk = 0;
0136 int settle_time = 60;
0137
0138
0139 if (argc == 2)
0140 disk = argv[1];
0141 else if (argc == 4) {
0142 settle_time = atoi(argv[2]);
0143 disk = argv[3];
0144 } else
0145 usage();
0146
0147 if (!(fd = open(disk, O_RDONLY|O_NONBLOCK))) {
0148 printf("Can't open %s, because: %s\n", disk, strerror(errno));
0149 exit(-1);
0150 }
0151
0152 if (settle_time) {
0153 printf("Waiting %d seconds for the system to settle down to "
0154 "'normal'\n", settle_time);
0155 sleep(settle_time);
0156 } else
0157 puts("Not waiting for system to settle down");
0158
0159 signal(SIGINT, ender);
0160
0161 measure(fd);
0162
0163 close(fd);
0164
0165 return 0;
0166 }