1.\" Copyright (c) 2005 Joseph Koshy. All rights reserved. 2.\" 3.\" Redistribution and use in source and binary forms, with or without 4.\" modification, are permitted provided that the following conditions 5.\" are met: 6.\" 1. Redistributions of source code must retain the above copyright 7.\" notice, this list of conditions and the following disclaimer. 8.\" 2. Redistributions in binary form must reproduce the above copyright 9.\" notice, this list of conditions and the following disclaimer in the 10.\" documentation and/or other materials provided with the distribution. 11.\" 12.\" This software is provided by Joseph Koshy ``as is'' and 13.\" any express or implied warranties, including, but not limited to, the 14.\" implied warranties of merchantability and fitness for a particular purpose 15.\" are disclaimed. in no event shall Joseph Koshy be liable 16.\" for any direct, indirect, incidental, special, exemplary, or consequential 17.\" damages (including, but not limited to, procurement of substitute goods 18.\" or services; loss of use, data, or profits; or business interruption) 19.\" however caused and on any theory of liability, whether in contract, strict 20.\" liability, or tort (including negligence or otherwise) arising in any way 21.\" out of the use of this software, even if advised of the possibility of 22.\" such damage. 23.\" 24.\" $FreeBSD$ 25.\" 26.Dd Jun 1, 2005 27.Os 28.Dt PMCLOG 3 29.Sh NAME 30.Nm pmclog_open , 31.Nm pmclog_close , 32.Nm pmclog_read , 33.Nm pmclog_feed 34.Nd parse event log data generated by 35.Xr hwpmc 4 36.Sh LIBRARY 37.Lb libpmc 38.Sh SYNOPSIS 39.In pmclog.h 40.Ft "void *" 41.Fn pmclog_open "int fd" 42.Ft void 43.Fn pmclog_close "void *cookie" 44.Ft int 45.Fn pmclog_read "void *cookie" "struct pmclog_ev *ev" 46.Ft int 47.Fn pmclog_feed "void *cookie" "char *data" "int len" 48.Sh DESCRIPTION 49These functions provide a way for application programs to extract 50events from an event stream generated by 51.Xr hwpmc 4 . 52.Pp 53A new event log parser is allocated using 54.Fn pmclog_open . 55Argument 56.Fa fd 57may be a file descriptor opened for reading if the event stream is 58present in a file, or the constant 59.Dv PMCLOG_FD_NONE 60for an event stream present in memory. 61This function returns a cookie that is passed into the other functions 62in this API set. 63.Pp 64Function 65.Fn pmclog_read 66returns the next available event in the event stream associated with 67argument 68.Fa cookie . 69Argument 70.Fa ev 71points to an event descriptor that which will contain the result of a 72successfully parsed event. 73.Pp 74An event descriptor returned by 75.Fn pmclog_read 76has the following structure: 77.Bd -literal 78struct pmclog_ev { 79 enum pmclog_state pl_state; /* parser state after 'get_event()' */ 80 off_t pl_offset; /* byte offset in stream */ 81 size_t pl_count; /* count of records so far */ 82 struct timespec pl_ts; /* log entry timestamp */ 83 enum pmclog_type pl_type; /* log entry kind */ 84 union { /* log entry data */ 85 struct pmclog_ev_allocate pl_a; 86 struct pmclog_ev_proccsw pl_c; 87 struct pmclog_ev_dropnotify pl_d; 88 struct pmclog_ev_procexit pl_e; 89 struct pmclog_ev_initialize pl_i; 90 struct pmclog_ev_pcsample pl_s; 91 struct pmclog_ev_pmcattach pl_t; 92 struct pmclog_ev_userdata pl_u; 93 struct pmclog_ev_procexec pl_x; 94 } pl_u; 95}; 96.Ed 97.Pp 98The current state of the parser is recorded in 99.Va pl_state . 100This field can take on the following values: 101.Bl -tag -width "PMCLOG_REQUIRE_DATA" -compact 102.It Dv PMCLOG_EOF 103.Pq For file based parsers only 104An end-of-file condition was encountered on the configured file 105descriptor. 106.It Dv PMCLOG_ERROR 107An error occurred during parsing. 108.It Dv PMCLOG_OK 109A complete event record was read into 110.Fa "*ev" . 111.It Dv PMCLOG_REQUIRE_DATA 112There was insufficient data in the event stream to assemble a complete 113event record. 114For memory based parsers, more data can be fed to the 115parser using function 116.Fn pmclog_feed . 117For file based parsers, function 118.Fn pmclog_read 119may be retried when data is available on the configured file 120descriptor. 121.El 122.Pp 123The rest of the event structure is valid only if field 124.Va pl_state 125contains 126.Dv PMCLOG_OK . 127Field 128.Va pl_offset 129contains the offset of the current record in the byte stream. 130Field 131.Va pl_count 132contains the serial number of this event. 133Field 134.Va pl_ts 135contains a timestamp with the system time when the event occurred. 136Field 137.Va pl_type 138denotes the kind of the event returned in argument 139.Fa *ev 140and is one of the following: 141.Bl -tag -width XXXXXXXXXXXXXXXXXXXXXXX -compact 142.It Dv PMCLOG_TYPE_DROPNOTIFY 143a marker indicating that 144.Xr hwpmc 4 145had to drop data due to a resource constraint. 146.It Dv PMCLOG_TYPE_INITIALIZE 147an initialization record. 148This is usually the first record in a log file. 149.It Dv PMCLOG_TYPE_PCSAMPLE 150A record containing an instruction pointer sample. 151.It Dv PMCLOG_TYPE_PMCALLOCATE 152A record describing a PMC allocation operation. 153.It Dv PMCLOG_TYPE_PMCATTACH 154A record describing a PMC attach operation. 155.It Dv PMCLOG_TYPE_PROCCSW 156A record describing a PMC reading at the time of a process context switch. 157.It Dv PMCLOG_TYPE_PROCEXIT 158A record describing the accumulated PMC reading for a process at the 159time of 160.Xr _exit 2 . 161.It Dv PMCLOG_TYPE_PROCEXEC 162A record describing an 163.Xr execve 2 164by a target process. 165.It Dv PMCLOG_TYPE_USERDATA 166A record containing user data. 167.El 168.Pp 169Function 170.Fn pmclog_feed 171is used with parsers configured to parse memory based event streams. 172It is intended to be called when function 173.Fn pmclog_read 174indicates the need for more data by a returning 175.Dv PMCLOG_REQUIRE_DATA 176in field 177.Va pl_state 178of its event structure argument. 179Argument 180.Fa data 181points to the start of a memory buffer containing fresh event data. 182Argument 183.Fa len 184indicates the number of bytes of data available. 185The memory range 186.Bq data , data+len 187must remain valid till the next time 188.Fn pmclog_read 189returns an error. 190It is an error to use 191.Fn pmclog_feed 192on a parser configured to parse file data. 193.Pp 194Function 195.Fn pmclog_close 196releases the internal state allocated by a prior call 197to 198.Fn pmclog_open . 199.Sh RETURN VALUES 200Function 201.Fn pmclog_open 202will return a non-NULL value if successful or NULL otherwise. 203.Pp 204Function 205.Fn pmclog_read 206will return 0 in case a complete event record was successfully read, 207or will return -1 and will set the 208.Va pl_state 209field of the event record to the appropriate code in case of an error. 210.Pp 211Function 212.Fn pmclog_feed 213will return 0 on success or -1 in case of failure. 214.Sh EXAMPLES 215A template for using the log file parsing API is shown below in psuedocode: 216.Bd -literal 217void *parser; /* cookie */ 218struct pmclog_ev ev; /* parsed event */ 219int fd; /* file descriptor */ 220 221fd = open(filename, O_RDONLY); /* open log file */ 222parser = pmclog_open(fd); /* initialize parser */ 223if (parser == NULL) 224 --handle an out of memory error--; 225 226/* read and parse data */ 227while (pmclog_read(parser, &ev) == 0) { 228 assert(ev.pl_state == PMCLOG_OK); 229 /* process the event */ 230 switch (ev.pl_type) { 231 case PMCLOG_TYPE_ALLOCATE: 232 --process a pmc allocation record-- 233 break; 234 case PMCLOG_TYPE_PROCCSW: 235 --process a thread context switch record-- 236 break; 237 case PMCLOG_TYPE_PCSAMPLE: 238 --process a PC sample-- 239 break; 240 --and so on-- 241 } 242} 243 244/* examine parser state */ 245switch (ev.pl_state) { 246case PMCLOG_EOF: 247 --normal termination-- 248 break; 249case PMCLOG_ERROR: 250 --look at errno here-- 251 break; 252case PMCLOG_REQUIRE_DATA: 253 --arrange for more data to be available for parsing-- 254 break; 255default: 256 assert(0); 257 /*NOTREACHED*/ 258} 259 260pmclog_close(parser); /* cleanup */ 261.Ed 262.Sh ERRORS 263A call to 264.Fn pmclog_init_parser 265may fail with any of the errors returned by 266.Xr malloc 3 . 267.Pp 268A call to 269.Fn pmclog_read 270for a file based parser may fail with any of the errors returned by 271.Xr read 2 . 272.Sh SEE ALSO 273.Xr read 2 , 274.Xr malloc 3 , 275.Xr pmc 3 , 276.Xr hwpmc 4 277