1 /* Copyright (c) 2013 The Chromium OS Authors. All rights reserved. 2 * Use of this source code is governed by a BSD-style license that can be 3 * found in the LICENSE file. 4 */ 5 6 #include <errno.h> 7 #include <stdio.h> 8 #include <string.h> 9 #include <syslog.h> 10 11 #include "cras_metrics.h" 12 #include "cras_main_message.h" 13 14 const char kNoCodecsFoundMetric[] = "Cras.NoCodecsFoundAtBoot"; 15 const char kStreamTimeoutMilliSeconds[] = "Cras.StreamTimeoutMilliSeconds"; 16 17 /* Type of metrics to log. */ 18 enum CRAS_SERVER_METRICS_TYPE { 19 LONGEST_FETCH_DELAY, 20 }; 21 22 struct cras_server_metrics_message { 23 struct cras_main_message header; 24 enum CRAS_SERVER_METRICS_TYPE metrics_type; 25 unsigned data; 26 }; 27 28 static void init_longest_fetch_delay_msg( 29 struct cras_server_metrics_message *msg, 30 enum CRAS_SERVER_METRICS_TYPE type, 31 unsigned data) 32 { 33 memset(msg, 0, sizeof(*msg)); 34 msg->header.type = CRAS_MAIN_METRICS; 35 msg->header.length = sizeof(*msg); 36 msg->metrics_type = type; 37 msg->data = data; 38 } 39 40 int cras_server_metrics_longest_fetch_delay(unsigned delay_msec) 41 { 42 struct cras_server_metrics_message msg; 43 int err; 44 45 init_longest_fetch_delay_msg(&msg, LONGEST_FETCH_DELAY, delay_msec); 46 err = cras_main_message_send((struct cras_main_message *)&msg); 47 if (err < 0) { 48 syslog(LOG_ERR, "Failed to send metrics message"); 49 return err; 50 } 51 52 return 0; 53 } 54 55 static void metrics_longest_fetch_delay(unsigned delay_msec) 56 { 57 static const int fetch_delay_min_msec = 1; 58 static const int fetch_delay_max_msec = 10000; 59 static const int fetch_delay_nbuckets = 10; 60 61 cras_metrics_log_histogram(kStreamTimeoutMilliSeconds, 62 delay_msec, 63 fetch_delay_min_msec, 64 fetch_delay_max_msec, 65 fetch_delay_nbuckets); 66 } 67 68 static void handle_metrics_message(struct cras_main_message *msg, void *arg) 69 { 70 struct cras_server_metrics_message *metrics_msg = 71 (struct cras_server_metrics_message *)msg; 72 switch (metrics_msg->metrics_type) { 73 case LONGEST_FETCH_DELAY: 74 metrics_longest_fetch_delay(metrics_msg->data); 75 break; 76 default: 77 syslog(LOG_ERR, "Unknown metrics type %u", 78 metrics_msg->metrics_type); 79 break; 80 } 81 82 } 83 84 int cras_server_metrics_init() { 85 cras_main_message_add_handler(CRAS_MAIN_METRICS, 86 handle_metrics_message, NULL); 87 return 0; 88 } 89