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.
|
|
|
|
|
|
|
|
/*
|
|
|
|
* metrics_library.cc
|
|
|
|
*
|
|
|
|
* Created on: Dec 1, 2009
|
|
|
|
* Author: sosa
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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";
|
|
|
|
static const char kChromePath[] =
|
|
|
|
"/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-04-16 01:40:23 +02:00
|
|
|
static void PrintError(const char *message, const char *file,
|
|
|
|
int code) {
|
2010-04-14 22:32:20 +02:00
|
|
|
const char *kProgramName = "metrics_library";
|
|
|
|
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-04-21 23:24:04 +02:00
|
|
|
// Sends message of size |length| to Chrome and returns true on success.
|
2010-04-16 01:40:23 +02:00
|
|
|
static bool SendMessageToChrome(int32_t length, const char *message) {
|
2010-04-14 22:32:20 +02:00
|
|
|
int chrome_fd = open(kChromePath,
|
|
|
|
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) {
|
|
|
|
PrintError("open", kChromePath, 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) {
|
|
|
|
PrintError("flock", kChromePath, errno);
|
|
|
|
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-04-14 22:32:20 +02:00
|
|
|
PrintError("write", kChromePath, 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-04-14 22:32:20 +02:00
|
|
|
PrintError("unlock", kChromePath, 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-04-21 23:24:04 +02:00
|
|
|
// Formats a name/value message for Chrome in |buffer| and returns the
|
2010-04-16 01:40:23 +02:00
|
|
|
// length of the message or a negative value on error.
|
|
|
|
//
|
|
|
|
// Message format is: | LENGTH(binary) | NAME | \0 | VALUE | \0 |
|
|
|
|
//
|
2010-04-21 23:24:04 +02:00
|
|
|
// The arbitrary |format| argument covers the non-LENGTH portion of the
|
2010-04-16 01:40:23 +02:00
|
|
|
// message. The caller is responsible to store the \0 character
|
|
|
|
// between NAME and VALUE (e.g. "%s%c%d", name, '\0', value).
|
|
|
|
static int32_t FormatChromeMessage(int32_t buffer_size, char *buffer,
|
|
|
|
const char *format, ...) {
|
|
|
|
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-04-21 23:24:04 +02:00
|
|
|
// static
|
|
|
|
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-04-21 23:24:04 +02:00
|
|
|
// static
|
|
|
|
bool MetricsLibrary::SendToChrome(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
|
|
|
}
|