WPILibC++ 2025.1.1
Loading...
Searching...
No Matches
timeprofile.h
Go to the documentation of this file.
1/* Copyright (C) 2013-2016, The Regents of The University of Michigan.
2All rights reserved.
3This software was developed in the APRIL Robotics Lab under the
4direction of Edwin Olson, ebolson@umich.edu. This software may be
5available under alternative licensing terms; contact the address above.
6Redistribution and use in source and binary forms, with or without
7modification, are permitted provided that the following conditions are met:
81. Redistributions of source code must retain the above copyright notice, this
9 list of conditions and the following disclaimer.
102. Redistributions in binary form must reproduce the above copyright notice,
11 this list of conditions and the following disclaimer in the documentation
12 and/or other materials provided with the distribution.
13THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
14ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
17ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23The views and conclusions contained in the software and documentation are those
24of the authors and should not be interpreted as representing official policies,
25either expressed or implied, of the Regents of The University of Michigan.
26*/
27
28#pragma once
29
30#ifdef __cplusplus
31extern "C" {
32#endif
33
34#include <stdio.h>
35#include <string.h>
36#include <stdint.h>
37
38#include "time_util.h"
39#include "zarray.h"
40
42{
43 char name[32];
44 int64_t utime;
45};
46
49{
50 int64_t utime;
52};
53
55{
56 timeprofile_t *tp = (timeprofile_t*) calloc(1, sizeof(timeprofile_t));
57 tp->stamps = zarray_create(sizeof(struct timeprofile_entry));
58
59 tp->utime = utime_now();
60
61 return tp;
62}
63
64static inline void timeprofile_destroy(timeprofile_t *tp)
65{
67 free(tp);
68}
69
70static inline void timeprofile_clear(timeprofile_t *tp)
71{
73 tp->utime = utime_now();
74}
75
76static inline void timeprofile_stamp(timeprofile_t *tp, const char *name)
77{
78 struct timeprofile_entry tpe;
79
80 strncpy(tpe.name, name, sizeof(tpe.name));
81 tpe.name[sizeof(tpe.name)-1] = 0;
82 tpe.utime = utime_now();
83
84 zarray_add(tp->stamps, &tpe);
85}
86
87static inline void timeprofile_display(timeprofile_t *tp)
88{
89 int64_t lastutime = tp->utime;
90
91 for (int i = 0; i < zarray_size(tp->stamps); i++) {
92 struct timeprofile_entry *stamp;
93
94 zarray_get_volatile(tp->stamps, i, &stamp);
95
96 double cumtime = (stamp->utime - tp->utime)/1000000.0;
97
98 double parttime = (stamp->utime - lastutime)/1000000.0;
99
100 printf("%2d %32s %15f ms %15f ms\n", i, stamp->name, parttime*1000, cumtime*1000);
101
102 lastutime = stamp->utime;
103 }
104}
105
106static inline uint64_t timeprofile_total_utime(timeprofile_t *tp)
107{
108 if (zarray_size(tp->stamps) == 0)
109 return 0;
110
111 struct timeprofile_entry *first, *last;
112 zarray_get_volatile(tp->stamps, 0, &first);
113 zarray_get_volatile(tp->stamps, zarray_size(tp->stamps) - 1, &last);
114
115 return last->utime - first->utime;
116}
117
118#ifdef __cplusplus
119}
120#endif
and restrictions which apply to each piece of software is included later in this file and or inside of the individual applicable source files The disclaimer of warranty in the WPILib license above applies to all code in and nothing in any of the other licenses gives permission to use the names of FIRST nor the names of the WPILib contributors to endorse or promote products derived from this software The following pieces of software have additional or alternate and or and nanopb were all modified for use in Google Inc All rights reserved Redistribution and use in source and binary with or without are permitted provided that the following conditions are this list of conditions and the following disclaimer *Redistributions in binary form must reproduce the above copyright this list of conditions and the following disclaimer in the documentation and or other materials provided with the distribution *Neither the name of Google Inc nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS AND ANY EXPRESS OR IMPLIED BUT NOT LIMITED THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY OR CONSEQUENTIAL WHETHER IN STRICT OR EVEN IF ADVISED OF THE POSSIBILITY OF SUCH January AND DISTRIBUTION Definitions License shall mean the terms and conditions for and distribution as defined by Sections through of this document Licensor shall mean the copyright owner or entity authorized by the copyright owner that is granting the License Legal Entity shall mean the union of the acting entity and all other entities that control are controlled by or are under common control with that entity For the purposes of this definition control direct or to cause the direction or management of such whether by contract or including but not limited to software source documentation and configuration files Object form shall mean any form resulting from mechanical transformation or translation of a Source including but not limited to compiled object generated and conversions to other media types Work shall mean the work of whether in Source or Object made available under the as indicated by a copyright notice that is included in or attached to the whether in Source or Object that is based or other modifications as a an original work of authorship For the purposes of this Derivative Works shall not include works that remain separable or merely the Work and Derivative Works thereof Contribution shall mean any work of including the original version of the Work and any modifications or additions to that Work or Derivative Works that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner For the purposes of this submitted means any form of or written communication sent to the Licensor or its including but not limited to communication on electronic mailing source code control and issue tracking systems that are managed or on behalf the Licensor for the purpose of discussing and improving the but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as Not a Contribution Contributor shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work Grant of Copyright License Subject to the terms and conditions of this each Contributor hereby grants to You a non no royalty free
Definition ThirdPartyNotices.txt:164
auto printf(string_view fmt, const T &... args) -> int
Formats args according to specifications in fmt and writes the output to stdout.
Definition printf.h:621
Definition timeprofile.h:42
char name[32]
Definition timeprofile.h:43
int64_t utime
Definition timeprofile.h:44
Definition timeprofile.h:49
int64_t utime
Definition timeprofile.h:50
zarray_t * stamps
Definition timeprofile.h:51
Definition zarray.h:44
int64_t utime_now(void)
static void timeprofile_clear(timeprofile_t *tp)
Definition timeprofile.h:70
static void timeprofile_display(timeprofile_t *tp)
Definition timeprofile.h:87
static void timeprofile_destroy(timeprofile_t *tp)
Definition timeprofile.h:64
static uint64_t timeprofile_total_utime(timeprofile_t *tp)
Definition timeprofile.h:106
static void timeprofile_stamp(timeprofile_t *tp, const char *name)
Definition timeprofile.h:76
static timeprofile_t * timeprofile_create(void)
Definition timeprofile.h:54
static void zarray_clear(zarray_t *za)
Removes all elements from the array and sets its size to zero.
Definition zarray.h:365
static void zarray_destroy(zarray_t *za)
Frees all resources associated with the variable array structure which was created by zarray_create()...
Definition zarray.h:70
static int zarray_size(const zarray_t *za)
Retrieves the number of elements currently being contained by the passed array, which may be differen...
Definition zarray.h:130
static void zarray_get_volatile(const zarray_t *za, int idx, void *p)
Similar to zarray_get(), but returns a "live" pointer to the internal storage, avoiding a memcpy.
Definition zarray.h:212
static zarray_t * zarray_create(size_t el_sz)
Creates and returns a variable array structure capable of holding elements of the specified size.
Definition zarray.h:57
static void zarray_add(zarray_t *za, const void *p)
Adds a new element to the end of the supplied array, and sets its value (by copying) from the data po...
Definition zarray.h:179