2010-04-14 22:32:20 +02:00
|
|
|
// Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "metrics_library.h"
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/file.h>
|
2010-04-16 01:40:23 +02:00
|
|
|
|
|
|
|
#include <cstdarg>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstring>
|
2010-04-14 22:32:20 +02:00
|
|
|
|
|
|
|
#define READ_WRITE_ALL_FILE_FLAGS \
|
|
|
|
(S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)
|
|
|
|
|
2010-04-21 23:24:04 +02:00
|
|
|
static const char kAutotestPath[] =
|
|
|
|
"/var/log/metrics/autotest-events";
|
2010-05-18 20:00:59 +02:00
|
|
|
static const char kUMAEventsPath[] =
|
2010-04-21 23:24:04 +02:00
|
|
|
"/var/log/metrics/uma-events";
|
2010-04-16 01:40:23 +02:00
|
|
|
static const int32_t kBufferSize = 1024;
|
2010-04-14 22:32:20 +02:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
// TODO(sosa@chromium.org) - use Chromium logger instead of stderr
|
2010-05-18 20:00:59 +02:00
|
|
|
static void PrintError(const char* message, const char* file,
|
2010-04-16 01:40:23 +02:00
|
|
|
int code) {
|
2010-05-18 20:00:59 +02:00
|
|
|
static const char *kProgramName = "libmetrics";
|
2010-04-14 22:32:20 +02:00
|
|
|
if (code == 0) {
|
|
|
|
fprintf(stderr, "%s: %s\n", kProgramName, message);
|
|
|
|
} else if (file == NULL) {
|
|
|
|
fprintf(stderr, "%s: ", kProgramName);
|
|
|
|
perror(message);
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "%s: %s: ", kProgramName, file);
|
|
|
|
perror(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-18 20:00:59 +02:00
|
|
|
MetricsLibrary::MetricsLibrary()
|
|
|
|
: uma_events_file_(NULL) {}
|
|
|
|
|
|
|
|
bool MetricsLibrary::SendMessageToChrome(int32_t length, const char* message) {
|
|
|
|
int chrome_fd = open(uma_events_file_,
|
2010-04-14 22:32:20 +02:00
|
|
|
O_WRONLY | O_APPEND | O_CREAT,
|
|
|
|
READ_WRITE_ALL_FILE_FLAGS);
|
2010-04-16 01:40:23 +02:00
|
|
|
// If we failed to open it, return.
|
2010-04-14 22:32:20 +02:00
|
|
|
if (chrome_fd < 0) {
|
2010-05-18 20:00:59 +02:00
|
|
|
PrintError("open", uma_events_file_, errno);
|
2010-04-16 01:40:23 +02:00
|
|
|
return false;
|
2010-04-14 22:32:20 +02:00
|
|
|
}
|
|
|
|
|
2010-04-16 01:40:23 +02:00
|
|
|
// Need to chmod because open flags are anded with umask. Ignore the
|
|
|
|
// exit code -- a chronos process may fail chmoding because the file
|
|
|
|
// has been created by a root process but that should be OK.
|
|
|
|
fchmod(chrome_fd, READ_WRITE_ALL_FILE_FLAGS);
|
2010-04-14 22:32:20 +02:00
|
|
|
|
2010-04-16 01:40:23 +02:00
|
|
|
// Grab an exclusive lock to protect Chrome from truncating
|
|
|
|
// underneath us. Keep the file locked as briefly as possible.
|
2010-04-14 22:32:20 +02:00
|
|
|
if (flock(chrome_fd, LOCK_EX) < 0) {
|
2010-05-18 20:00:59 +02:00
|
|
|
PrintError("flock", uma_events_file_, errno);
|
2010-04-14 22:32:20 +02:00
|
|
|
close(chrome_fd);
|
2010-04-16 01:40:23 +02:00
|
|
|
return false;
|
2010-04-14 22:32:20 +02:00
|
|
|
}
|
|
|
|
|
2010-04-16 01:40:23 +02:00
|
|
|
bool success = true;
|
|
|
|
if (write(chrome_fd, message, length) != length) {
|
2010-05-18 20:00:59 +02:00
|
|
|
PrintError("write", uma_events_file_, errno);
|
2010-04-16 01:40:23 +02:00
|
|
|
success = false;
|
|
|
|
}
|
2010-04-14 22:32:20 +02:00
|
|
|
|
2010-04-16 01:40:23 +02:00
|
|
|
// Release the file lock and close file.
|
|
|
|
if (flock(chrome_fd, LOCK_UN) < 0) {
|
2010-05-18 20:00:59 +02:00
|
|
|
PrintError("unlock", uma_events_file_, errno);
|
2010-04-16 01:40:23 +02:00
|
|
|
success = false;
|
|
|
|
}
|
2010-04-14 22:32:20 +02:00
|
|
|
close(chrome_fd);
|
2010-04-16 01:40:23 +02:00
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2010-05-18 20:00:59 +02:00
|
|
|
int32_t MetricsLibrary::FormatChromeMessage(int32_t buffer_size, char* buffer,
|
|
|
|
const char* format, ...) {
|
2010-04-16 01:40:23 +02:00
|
|
|
int32_t message_length;
|
|
|
|
size_t len_size = sizeof(message_length);
|
|
|
|
|
|
|
|
// Format the non-LENGTH contents in the buffer by leaving space for
|
|
|
|
// LENGTH at the start of the buffer.
|
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
|
|
|
message_length = vsnprintf(&buffer[len_size], buffer_size - len_size,
|
|
|
|
format, args);
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
if (message_length < 0) {
|
|
|
|
PrintError("chrome message format error", NULL, 0);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// +1 to account for the trailing \0.
|
|
|
|
message_length += len_size + 1;
|
|
|
|
if (message_length > buffer_size) {
|
|
|
|
PrintError("chrome message too long", NULL, 0);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prepend LENGTH to the message.
|
|
|
|
memcpy(buffer, &message_length, len_size);
|
|
|
|
return message_length;
|
|
|
|
}
|
|
|
|
|
2010-05-12 22:05:45 +02:00
|
|
|
void MetricsLibrary::Init() {
|
2010-05-18 20:00:59 +02:00
|
|
|
uma_events_file_ = kUMAEventsPath;
|
2010-05-12 22:05:45 +02:00
|
|
|
}
|
|
|
|
|
2010-04-21 23:24:04 +02:00
|
|
|
bool MetricsLibrary::SendToAutotest(const string& name, int value) {
|
2010-04-16 01:40:23 +02:00
|
|
|
FILE *autotest_file = fopen(kAutotestPath, "a+");
|
|
|
|
if (autotest_file == NULL) {
|
|
|
|
PrintError("fopen", kAutotestPath, errno);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(autotest_file, "%s=%d\n", name.c_str(), value);
|
|
|
|
fclose(autotest_file);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-05-13 00:26:16 +02:00
|
|
|
bool MetricsLibrary::SendToUMA(const string& name, int sample,
|
|
|
|
int min, int max, int nbuckets) {
|
2010-04-16 01:40:23 +02:00
|
|
|
// Format the message.
|
|
|
|
char message[kBufferSize];
|
|
|
|
int32_t message_length =
|
|
|
|
FormatChromeMessage(kBufferSize, message,
|
2010-04-21 23:24:04 +02:00
|
|
|
"histogram%c%s %d %d %d %d", '\0',
|
|
|
|
name.c_str(), sample, min, max, nbuckets);
|
2010-04-16 01:40:23 +02:00
|
|
|
|
|
|
|
if (message_length < 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Send the message.
|
|
|
|
return SendMessageToChrome(message_length, message);
|
2010-04-14 22:32:20 +02:00
|
|
|
}
|
2010-04-22 00:45:10 +02:00
|
|
|
|
2010-05-13 00:26:16 +02:00
|
|
|
bool MetricsLibrary::SendEnumToUMA(const std::string& name, int sample,
|
|
|
|
int max) {
|
2010-04-22 00:45:10 +02:00
|
|
|
// Format the message.
|
|
|
|
char message[kBufferSize];
|
|
|
|
int32_t message_length =
|
|
|
|
FormatChromeMessage(kBufferSize, message,
|
|
|
|
"linearhistogram%c%s %d %d", '\0',
|
|
|
|
name.c_str(), sample, max);
|
|
|
|
|
|
|
|
if (message_length < 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Send the message.
|
|
|
|
return SendMessageToChrome(message_length, message);
|
|
|
|
}
|