| 1 | #ifndef _DEVEL_STATPROFILER_TRACEFILE | ||
| 2 | #define _DEVEL_STATPROFILER_TRACEFILE | ||
| 3 | |||
| 4 | #include "EXTERN.h" | ||
| 5 | #include "perl.h" | ||
| 6 | |||
| 7 | #include <string> | ||
| 8 | #if defined(_WIN32) | ||
| 9 | #include "wincompat.h" | ||
| 10 | #endif | ||
| 11 | |||
| 12 | #include <cstdio> | ||
| 13 | |||
| 14 | #include "thx_member.h" | ||
| 15 | |||
| 16 | #define ID_SIZE 6 | ||
| 17 | // bigger buffers compress better | ||
| 18 | #if SNAPPY | ||
| 19 | #define OUTPUT_BUFFER_SIZE 32000 | ||
| 20 | #else | ||
| 21 | #define OUTPUT_BUFFER_SIZE 8192 | ||
| 22 | #endif | ||
| 23 | |||
| 24 | namespace devel { | ||
| 25 | namespace statprofiler { | ||
| 26 | struct PerlVersion_t { | ||
| 27 | int revision; | ||
| 28 | int version; | ||
| 29 | int subversion; | ||
| 30 | }; | ||
| 31 | |||
| 32 | struct Genealogy_t { | ||
| 33 | char id[ID_SIZE * 4 * 2 + 1], parent_id[ID_SIZE * 4 * 2 + 1]; | ||
| 34 | unsigned int ordinal, parent_ordinal; | ||
| 35 | }; | ||
| 36 | |||
| 37 | enum FrameType | ||
| 38 | { | ||
| 39 | FRAME_SUB, | ||
| 40 | FRAME_XSUB, | ||
| 41 | FRAME_MAIN, | ||
| 42 | FRAME_EVAL, | ||
| 43 | }; | ||
| 44 | |||
| 45 | class SnappyInput; | ||
| 46 | class SnappyOutput; | ||
| 47 | |||
| 48 | class InputBuffer | ||
| 49 | { | ||
| 50 | public: | ||
| 51 | InputBuffer(); | ||
| 52 | ~InputBuffer(); | ||
| 53 | |||
| 54 | void skip_bytes(size_t size); | ||
| 55 | int read_byte(); | ||
| 56 | void read_bytes(void *buffer, size_t size); | ||
| 57 | |||
| 58 | bool is_valid() const { return fh; } | ||
| 59 | |||
| 60 | void open(std::FILE *fh); | ||
| 61 | void close(); | ||
| 62 | |||
| 63 | int read_raw_byte(); | ||
| 64 | bool read_raw_bytes(void *buffer, size_t size); | ||
| 65 | |||
| 66 | private: | ||
| 67 | void fill_buffer(); | ||
| 68 | |||
| 69 | #if SNAPPY | ||
| 70 | SnappyInput *snappy; | ||
| 71 | #endif | ||
| 72 | std::FILE *fh; | ||
| 73 | char input_buffer[OUTPUT_BUFFER_SIZE]; | ||
| 74 | char *input_position, *input_end; | ||
| 75 | }; | ||
| 76 | |||
| 77 | class OutputBuffer | ||
| 78 | { | ||
| 79 | public: | ||
| 80 | OutputBuffer(); | ||
| 81 | ~OutputBuffer(); | ||
| 82 | |||
| 83 | int write_bytes(const void *bytes, size_t size); | ||
| 84 | int write_byte(const char byte); | ||
| 85 | int flush(); | ||
| 86 | |||
| 87 | bool is_valid() const { return fh; } | ||
| 88 | |||
| 89 | void open(std::FILE *fh); | ||
| 90 | void close(); | ||
| 91 | |||
| 92 | int position() const; | ||
| 93 | |||
| 94 | bool write_raw_byte(int c); | ||
| 95 | bool write_raw_bytes(const void *buffer, size_t size); | ||
| 96 | |||
| 97 | private: | ||
| 98 | int flush_buffer(); | ||
| 99 | |||
| 100 | #if SNAPPY | ||
| 101 | SnappyOutput *snappy; | ||
| 102 | #endif | ||
| 103 | std::FILE *fh; | ||
| 104 | char output_buffer[OUTPUT_BUFFER_SIZE]; | ||
| 105 | char *output_position; | ||
| 106 | }; | ||
| 107 | |||
| 108 | class TraceFileReader | ||
| 109 | { | ||
| 110 | public: | ||
| 111 | // Note: Constructor does not open the file! | ||
| 112 | TraceFileReader(pTHX_ SV *mapper); | ||
| 113 | ~TraceFileReader(); | ||
| 114 | |||
| 115 | void open(const std::string &path); | ||
| 116 | void close(); | ||
| 117 | bool is_valid() const { return in.is_valid(); } | ||
| 118 | |||
| 119 | 1 | unsigned int get_format_version() const { return file_format_version; } | |
| 120 | 9998 | const PerlVersion_t& get_source_perl_version() const { return source_perl_version; } | |
| 121 | 16500 | const Genealogy_t& get_genealogy_info() const { return genealogy_info; } | |
| 122 | 9999 | int get_source_tick_duration() const { return source_tick_duration; } | |
| 123 | 1 | int get_source_stack_sample_depth() const { return source_stack_sample_depth; } | |
| 124 | |||
| 125 | SV *read_trace(); | ||
| 126 | // Returns the hash of custom meta data records that have been encountered | ||
| 127 | // thus far. | ||
| 128 | HV *get_custom_metadata(); | ||
| 129 | void clear_custom_metadata(); | ||
| 130 | void delete_custom_metadata(AV *keys); | ||
| 131 | |||
| 132 | // Returns the source code that has been collected thus far | ||
| 133 | HV *get_source_code(); | ||
| 134 | |||
| 135 | // Returns currently-open sections | ||
| 136 | HV *get_active_sections(); | ||
| 137 | |||
| 138 | 252 | bool is_file_ended() const { return file_ended; } | |
| 139 | 8494 | bool is_stream_ended() const { return stream_ended; } | |
| 140 | |||
| 141 | private: | ||
| 142 | void read_header(); | ||
| 143 | void read_custom_meta_record(const int size, HV *extra_output_hash = NULL); | ||
| 144 | bool can_map_evals(); | ||
| 145 | SV *map_name(SV *package, SV *name); | ||
| 146 | SV *map_eval(SV *eval_name); | ||
| 147 | SV *maybe_map_eval(SV *eval_name); | ||
| 148 | |||
| 149 | InputBuffer in; | ||
| 150 | // TODO maybe introduce a header struct or class for cleanliness? | ||
| 151 | unsigned int file_format_version; | ||
| 152 | PerlVersion_t source_perl_version; | ||
| 153 | Genealogy_t genealogy_info; | ||
| 154 | int source_tick_duration; | ||
| 155 | int source_stack_sample_depth; | ||
| 156 | HV *custom_metadata, *source_code; | ||
| 157 | HV *sections; | ||
| 158 | // various stashes used by the reader | ||
| 159 | HV *st_stash, *sf_stash, *msf_stash, *esf_stash; | ||
| 160 | bool sections_changed, metadata_changed; | ||
| 161 | bool stream_ended, file_ended; | ||
| 162 | SV *mapper; | ||
| 163 | HV *sub_prefix_rx; | ||
| 164 | bool map_evals; | ||
| 165 | |||
| 166 | DECL_THX_MEMBER | ||
| 167 | }; | ||
| 168 | |||
| 169 | class TraceFileWriter | ||
| 170 | { | ||
| 171 | public: | ||
| 172 | static const bool write_end_tag = true; | ||
| 173 | |||
| 174 | // Usage in this order: Construct object, open, write_header, write samples | ||
| 175 | TraceFileWriter(pTHX); | ||
| 176 | ~TraceFileWriter(); | ||
| 177 | |||
| 178 | int open(const std::string &path, bool is_template, uint32_t id[ID_SIZE], unsigned int ordinal); | ||
| 179 | void close(bool write_end_tag = false); | ||
| 180 | void shut(); | ||
| 181 | void flush(); | ||
| 182 | long position() const; | ||
| 183 | |||
| 184 | bool is_valid() const { return out.is_valid(); } | ||
| 185 | |||
| 186 | int write_header(unsigned int sampling_interval, | ||
| 187 | unsigned int stack_collect_depth, | ||
| 188 | uint32_t id[ID_SIZE], unsigned int ordinal, | ||
| 189 | uint32_t parent_id[ID_SIZE], unsigned int parent_ordinal, | ||
| 190 | HV *global_metadata); | ||
| 191 | |||
| 192 | int start_sample(unsigned int weight, const char *op_name); | ||
| 193 | int add_frame(FrameType frame_type, CV *sub, GV *sub_name, COP *line); | ||
| 194 | int add_eval_source(SV *eval_text, U32 eval_seq); | ||
| 195 | int end_sample(); | ||
| 196 | |||
| 197 | int write_custom_metadata(SV *key, SV *value); | ||
| 198 | int start_section(SV *section_name); | ||
| 199 | int end_section(SV *section_name); | ||
| 200 | |||
| 201 | private: | ||
| 202 | int write_perl_version(); | ||
| 203 | int write_custom_metadata(const char *key, I32 klen, SV *value); | ||
| 204 | |||
| 205 | OutputBuffer out; | ||
| 206 | std::string output_file; | ||
| 207 | bool force_empty_frame; | ||
| 208 | |||
| 209 | DECL_THX_MEMBER | ||
| 210 | }; | ||
| 211 | } | ||
| 212 | } | ||
| 213 | |||
| 214 | #ifdef _DEVEL_STATPROFILER_XSP | ||
| 215 | using namespace devel::statprofiler; | ||
| 216 | #endif | ||
| 217 | |||
| 218 | #endif |