1
Fork 0
mirror of git://git.sv.gnu.org/emacs.git synced 2026-01-07 04:10:27 -08:00
emacs/mps/code/eventpy.c
Gareth Rees 2e85d9a850 Don't store booleans as bit-fields in event structures: this makes it impossible to automatically unpack them using python's struct module. (the only event where this actually saved any space was poolinitmvff!)
Update event format to 2.0.0 since telemetry format changed.
New macro EVENT_ANY_FIELDS allows consumers to determine the layout of the event header.
mpseventpy now analyzes the event layout (including padding bytes) and emits struct format specifications.
New program tool/monitor.py reads and unpacks a stream of telemetry events.

Copied from Perforce
 Change: 194026
2018-06-20 15:51:38 +01:00

187 lines
6.5 KiB
C

/* eventpy.c: GENERATE PYTHON INTERFACE TO EVENTS
*
* $Id$
* Copyright (c) 2016-2018 Ravenbrook Limited. See end of file for license.
*
* This command-line program emits Python data structures that can be
* used to parse an event stream in text format (as output by the
* mpseventcnv program).
*/
#include <assert.h> /* assert */
#include <stddef.h> /* offsetof */
#include <stdio.h> /* printf, puts */
#include "event.h"
static void format(size_t size, const char *sort)
{
switch (sort[0]) {
case 'D':
printf("d");
break;
case 'S':
printf("s");
break;
case 'B':
printf("?");
break;
default:
switch (size) {
case 1:
printf("B");
break;
case 2:
printf("H");
break;
case 4:
printf("L");
break;
case 8:
printf("Q");
break;
default:
assert(FALSE);
break;
}
}
}
int main(int argc, char *argv[])
{
size_t size, prev_offset;
UNUSED(argc);
UNUSED(argv);
puts("from collections import namedtuple");
printf("\n__version__ = %d, %d, %d\n", EVENT_VERSION_MAJOR,
EVENT_VERSION_MEDIAN, EVENT_VERSION_MINOR);
puts("\n# Description of an event kind.");
puts("KindDesc = namedtuple('KindDesc', 'name code doc')");
puts("\n# Namespace containing a KindDesc for every kind.");
puts("class Kind:");
#define ENUM(_, NAME, DOC) \
printf(" " #NAME " = KindDesc('" #NAME "', %d, '%s')\n", \
EventKind ## NAME, DOC);
EventKindENUM(ENUM, _);
#undef ENUM
puts("\n# Mapping from kind number to KindDesc.");
puts("KIND = {");
#define ENUM(_, NAME, _1) \
printf(" %d: Kind." #NAME ",\n", EventKind ## NAME);
EventKindENUM(ENUM, _);
#undef ENUM
puts("}");
puts("\n# Description of a parameter of an event.");
puts("EventParam = namedtuple('EventParam', 'sort name')");
puts("\n# Description of the parameters of an event.");
puts("EventDesc = namedtuple('EventDesc', "
"'name code always kind params maxsize format')");
puts("\n# Namespace containing an EventDesc for every event.");
puts("class Event:");
#define PAD_TO(offset) \
if (prev_offset < offset) \
printf("%ux", (unsigned)(offset - prev_offset)); \
prev_offset = offset;
#define EVENT_PARAM(X, INDEX, SORT, NAME) \
puts(" EventParam('" #SORT "', '" #NAME "'),");
#define EVENT_FORMAT(STRUCTNAME, INDEX, SORT, NAME) \
PAD_TO(offsetof(Event##STRUCTNAME##Struct, f##INDEX)); \
format(sizeof(EventF##SORT), #SORT); \
prev_offset += sizeof(EventF##SORT);
#define EVENT_DEFINE(X, NAME, CODE, ALWAYS, KIND) \
printf(" " #NAME " = EventDesc('" #NAME "', %d, %s, Kind." #KIND ", [\n", \
CODE, ALWAYS ? "True" : "False"); \
EVENT_ ## NAME ## _PARAMS(EVENT_PARAM, X); \
size = sizeof(Event##NAME##Struct) - sizeof(EventAnyStruct); \
printf(" ], %u, '=", (unsigned)size); \
prev_offset = sizeof(EventAnyStruct); \
EVENT_ ## NAME ## _PARAMS(EVENT_FORMAT, NAME); \
PAD_TO(sizeof(Event##NAME##Struct)); \
puts("')");
EVENT_LIST(EVENT_DEFINE, 0);
#undef EVENT_DEFINE
#undef EVENT_PARAM
#undef EVENT_FORMAT
puts("\n# Mapping from event number to EventDesc.");
puts("EVENT = {");
#define EVENT_ITEM(X, NAME, CODE, ALWAYS, KIND) \
printf(" %d: Event." #NAME ",\n", CODE);
EVENT_LIST(EVENT_ITEM, 0);
#undef EVENT_ITEM
puts("}");
puts("\n# Description of an event header.");
printf("EventAnyDesc = namedtuple('EventAnyDesc', '");
#define EVENT_FIELD(type, name) printf("%s ", #name);
EVENT_ANY_FIELDS(EVENT_FIELD)
#undef EVENT_FIELD
puts("')");
puts("\n# Size of event header in bytes.");
printf("EVENT_ANY_SIZE = %u\n", (unsigned)sizeof(EventAnyStruct));
puts("\n# Struct format for event header.");
printf("EVENT_ANY_FORMAT = '=");
prev_offset = 0;
#define EVENT_FIELD(type, name) \
PAD_TO(offsetof(EventAnyStruct, name)); \
format(sizeof(type), "?"); \
prev_offset += sizeof(type);
EVENT_ANY_FIELDS(EVENT_FIELD)
#undef EVENT_FIELD
PAD_TO(sizeof(EventAnyStruct));
puts("'");
return 0;
}
/* C. COPYRIGHT AND LICENSE
*
* Copyright (c) 2016-2018 Ravenbrook Limited <http://www.ravenbrook.com/>.
* All rights reserved. This is an open source license. Contact
* Ravenbrook for commercial licensing options.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Redistributions in any form must be accompanied by information on how
* to obtain complete source code for this software and any accompanying
* software that uses this software. The source code must either be
* included in the distribution or be available for no more than the cost
* of distribution plus a nominal fee, and must be freely redistributable
* under reasonable conditions. For an executable file, complete source
* code means the source code for all modules it contains. It does not
* include source code for modules or files that typically accompany the
* major components of the operating system on which the executable file
* runs.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
* PURPOSE, OR NON-INFRINGEMENT, ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/