source: trunk/markup_stats.cxx @ 161

Last change on this file since 161 was 161, checked in by cameron, 11 years ago

markup_stats fix: nonexistent file.

File size: 14.5 KB
Line 
1/*  markup_stats.c - parabix demo program
2    Copyright (c) 2007, Robert D. Cameron.
3    Licensed to the public under the Open Software License 3.0.
4    Licensed to International Characters, Inc., under the Academic
5    Free License 3.0.
6
7*/
8
9#include <stdio.h>
10#include <stdlib.h>
11#include <string.h>
12#include <errno.h>
13#include <sys/types.h>
14#include <sys/stat.h>
15#include <string>
16#include <iostream>
17using namespace std;
18
19#define ON 1
20#define OFF 2
21
22#define VALIDATION_MODE OFF
23
24#ifdef PAPI
25#include "../code_clocker/clocker/cc.h"
26#include "../code_clocker/clocker/cc.cxx"
27
28CC * code_clocker;
29
30#define NONE 0
31#define END_TAG_MATCHING 1
32#define ATTRIBUTE_UNIQUENESS 2
33#define NAME_VALIDATION 3
34#define NAME_LOOKUP 4
35
36#define OMISSION NONE
37
38
39#define CHARSET_VALIDATION 1
40#define WS_CONTROL 2
41#define MARKUP_STREAMS 3
42#define BITLEX_ALL 4
43#define BITPLEX 5
44#define BYTEPLEX 6
45#define ADVANCE_BUFFERS 7
46#define BUFFER_TOTAL 8
47#define FILE_READING 9
48
49#define CODE_CLOCKING BUFFER_TOTAL
50
51#endif
52
53//#include "src/ilax.h"
54#include "src/engine.h"
55
56#ifndef REPEAT_RUNS
57#define REPEAT_RUNS 1
58#endif
59
60/* Internals */
61#include "src/xmlmodel.h"
62#include "src/xml_error.h"
63#include "src/bitplex.h"
64#include "src/byteplex.h"
65#include "src/xmldecl.h"
66#include "src/bitlex.h"
67
68
69#include "src/xmlmodel.c"
70#include "src/xml_error.c"
71#include "src/bitplex.c"
72#include "src/byteplex.c"
73#include "src/xmldecl.c"
74#include "src/bitlex.c"
75#include "src/engine.c"
76#include "src/symtab.c"
77
78/* Global declarations of parsing engine. */
79Parser_Interface * parser;
80
81/* Global declarations for statistics. */
82
83int comment_count = 0;
84int comment_length = 0;
85int CDATA_start_count = 0;
86int CDATA_start_pos = 0;
87int CDATA_length = 0;
88int CDATA_end_count = 0;
89int PI_count = 0;
90int PI_length = 0;
91int empty_elem_count = 0;
92int empty_elem_length = 0;
93int start_tag_count = 0;
94int start_tag_length = 0;
95int attribute_count = 0;
96int end_tag_count = 0;
97int end_tag_length = 0;
98int reference_count = 0;
99int reference_length = 0;
100int text_item_count = 0;
101int text_item_length = 0;
102int error_item_count = 0;
103int error_item_length = 0;
104int nesting_depth = 0;
105int max_nesting_depth = 0;
106int total_attribute_count = 0;
107int total_att_name_length = 0;
108int total_att_value_length = 0;
109int namespace_count = 0;
110int total_namespace_name_length = 0;
111int total_namespace_URI_length = 0;
112
113int last_item_start = 0;
114int last_item_stop = 0;
115int last_buffer_rel_pos = 0;
116
117
118/* Action routine for an XML comment in "<!--"  "-->" brackets. */
119template <class B>
120inline void ParsingEngine<B>::Comment_action(unsigned char * item, int lgth) {
121        comment_count +=1;
122       
123        #if defined(CALC_AVG)
124                comment_length += lgth;
125        #endif
126}
127
128/* Action routine called upon recognizing "<![CDATA[" to start a CDATA section. */
129template <class B>
130inline void ParsingEngine<B>::CDATA_start_action(unsigned char * CDATA_ptr){
131        CDATA_start_pos = AbsPos() - 9;
132        CDATA_start_count +=1;
133}
134
135/* Action routine called upon recognizing "]]>" to end a CDATA section. */
136template <class B>
137inline void ParsingEngine<B>::CDATA_end_action(unsigned char * CDATA_end_ptr) {
138        CDATA_end_count +=1;
139       
140        #if defined(CALC_AVG)
141                CDATA_length += AbsPos() - CDATA_start_pos;
142        #endif
143}
144
145/* Action routine for an XML processing instruction enclosed in "<?" and "?>" brackets. */
146template <class B>
147inline void ParsingEngine<B>::PI_action(unsigned char * item, int lgth) {
148        PI_count +=1;
149       
150        #if defined(CALC_AVG)
151                PI_length += lgth;
152        #endif
153}
154
155/* Action routine for an empty element enclosed in "<" and "/>" brackets. */
156template <class B>
157inline void ParsingEngine<B>::EmptyElement_action(unsigned char * item, int lgth) {
158        empty_elem_count +=1;
159       
160        #if defined(CALC_AVG)
161                empty_elem_length += lgth;
162        #endif
163}
164
165/* Action routine for a start tag enclosed in "<" and ">" brackets. */
166template <class B>
167inline void ParsingEngine<B>::StartTag_action(unsigned char * item, int lgth) {
168        start_tag_count +=1;
169       
170        #if defined(CALC_AVG)
171                start_tag_length += lgth;
172        #endif
173       
174        nesting_depth += 1;
175        if (nesting_depth > max_nesting_depth) max_nesting_depth = nesting_depth;
176//      cout << string((char *) item, lgth) << endl;
177}
178
179/* Action routine for an end tag enclosed in "</" and ">" brackets. */
180template <class B>
181inline void ParsingEngine<B>::EndTag_action(unsigned char * item, int lgth) {
182        end_tag_count +=1;
183       
184        #if defined(CALC_AVG)
185                end_tag_length += lgth;
186        #endif
187       
188        nesting_depth -= 1;
189}
190
191/* Action routine for an error item */
192template <class B>
193inline void ParsingEngine<B>::Error_action(unsigned char * item, int lgth) {
194        error_item_count +=1;
195       
196        #if defined(CALC_AVG)
197                error_item_length += lgth;
198        #endif
199       
200        fprintf(stderr, "Error: illegal markup at positions %i of length %i.\n", AbsPos()-lgth, lgth);
201        cerr << string((char *) item, lgth) << endl;
202}
203
204/* Action routine for a text item */
205template <class B>
206inline void ParsingEngine<B>::Text_action(unsigned char * item, int lgth) {
207        text_item_count +=1;
208       
209        #if defined(CALC_AVG)
210                text_item_length += lgth;
211        #endif
212}
213
214template <class B>
215inline void ParsingEngine<B>::Reference_action(unsigned char * item, int lgth) {
216        reference_count +=1;
217       
218        #if defined(CALC_AVG)
219                reference_length += lgth;
220        #endif
221}
222
223
224
225
226/* Three action routines for markup components are defined as follows.
227|ElementName_action| is the action routine called upon recognition of
228an element name immediately after the opening angle bracket of a start
229tag or empty element tag.  {\bf OR MAYBE THIS SHOULD BE DEFERRED UNTIL
230AFTER ATTRIBUTE PROCESSING SO THAT NAMESPACES ARE SET?}
231It is called with two parameters identifying the
232first and last character positions of the expected XML_name.
233Similarly, |PI_Target_action| is the action routine called upon recognition
234of the XML Name that occurs immediately after the opening "<?"
235delimiter of a processing instruction.
236
237 The third action routine for markup components is Attribute_Value_action,
238which takes three parameters rather than two.  {\bf OR POSSIBLY JUST
239THE QUOTE MARK ITEMS, RELYING ON THE END OF THE LAST COMPONENT PROCESSED
240TO MARK THE SPACE BEFORE THE ATT NAME.- REQUIRES ELEMENT_NAME_ACTION}
241*/
242
243/* Semantic action routines for markup components. */
244/* Action routine for an element name occurring immediately after the
245   opening "<" of a start tag or empty element tag. */
246template <class B>
247inline void ParsingEngine<B>::ElementName_action(unsigned char * item, int lgth) {
248}
249
250/* Action routine for a processing instruction target name occurring immediately
251   after the opening "<?" of a processing instruction. */
252template <class B>
253inline void ParsingEngine<B>::PI_Target_action(unsigned char * item, int lgth) {
254}
255
256/* Action routine for an individual attribute/value pair occurring in
257   a element start tag or an empty element tag. */
258template <class B>
259inline void ParsingEngine<B>::AttributeValue_action(unsigned char * name, int name_lgth, 
260                                 unsigned char * val, int val_lgth) {
261        total_attribute_count+=1;
262       
263        #if defined(CALC_AVG)
264                total_att_name_length += name_lgth;
265                total_att_value_length += val_lgth;
266        #endif
267}
268
269/* Action routine for an individual attribute/value pair occurring in
270   a element start tag or an empty element tag. */
271template <class B>
272inline void ParsingEngine<B>::Namespace_action(unsigned char * name, int name_lgth,
273                             unsigned char * URI, int URI_lgth) {
274        namespace_count+=1;
275       
276        #if defined(CALC_AVG)
277                total_namespace_name_length += name_lgth;
278                total_namespace_URI_length += URI_lgth;
279        #endif
280}
281
282
283template <class B>
284inline void ParsingEngine<B>::FinalizeBuffer_action() {
285       
286       
287#ifdef DEBUG
288        printf ("FinalizeBuffer; last 16 bytes + lookahead 16 =\n");
289        cout << string((char *) GetCodeUnitPtr(AbsPos()-16), 16) << "::" << string((char *) GetCodeUnitPtr(AbsPos()), 16) << endl;
290#endif
291        #if defined(PAPI) and defined(CODE_CLOCKING) and (CODE_CLOCKING == BUFFER_TOTAL)
292                code_clocker->cc_end_interval(BUFFER_SIZE);
293        #endif
294        #if defined(PAPI) and defined(CODE_CLOCKING) and (CODE_CLOCKING == BUFFER_TOTAL)
295                code_clocker->cc_start_interval();
296        #endif
297       
298}
299
300
301template <class B>
302inline void ParsingEngine<B>::DocumentStart_action() {
303        #if defined(PAPI) and defined(CODE_CLOCKING) and (CODE_CLOCKING == BUFFER_TOTAL)
304                code_clocker->cc_start_interval();
305        #endif
306}
307
308template <class B>
309inline void ParsingEngine<B>::DocumentEnd_action() {
310        #if defined(PAPI) and defined(CODE_CLOCKING) and (CODE_CLOCKING == BUFFER_TOTAL)
311                code_clocker->cc_end_interval(buffer_rel_pos);
312        #endif
313}
314
315template <class B>
316inline void ParsingEngine<B>::Doctype_action(unsigned char * item, int lgth) {
317#ifdef SHOW_DTD_ACTIONS
318        printf("Document Type:\n");
319        cout << string((char *) item, lgth) <<endl;
320#endif
321}
322
323template <class B>
324inline void ParsingEngine<B>::PEReference_action(unsigned char * item, int lgth) {
325}
326
327
328template <class B>
329inline void ParsingEngine<B>::ExtSubsetDecl_action(unsigned char * item, int lgth) {
330#ifdef SHOW_DTD_ACTIONS
331        printf("ExtSubsetDecl:\n");
332        cout << string((char *) item, lgth) <<endl;
333#endif
334}
335
336template <class B>
337inline void ParsingEngine<B>::Prolog_action(unsigned char * item, int lgth) {
338#ifdef SHOW_DTD_ACTIONS
339        printf("Prolog:\n");
340        cout << string((char *) item, lgth) <<endl;
341#endif
342}
343
344#define print_stats(stat_string, count, total_lgth) \
345        printf("%i %s", count, stat_string);\
346        if (count == 0) printf("s.\n");\
347        else if (count == 1) printf(" of length %i.\n", total_lgth);\
348        else printf("s of avg. lgth %i.\n", total_lgth/count);
349
350
351
352
353int
354main(int argc, char * argv[]) {
355        if (argc != 2) {
356        printf("Usage: %s <filename>\n", argv[0]);
357                exit(-1);
358        }
359       
360        char * src_filename = argv[1];
361        char * cmdline = new char[strlen(argv[0]) + strlen(argv[1]) +1 +1]; 
362        strcat(cmdline, argv[0]);
363        strcat(cmdline," ");
364        strcat(cmdline,argv[1]);
365
366        #ifdef PAPI
367                #define NUM_EVENTS 2
368                int Events[NUM_EVENTS] = {PAPI_TOT_CYC, PAPI_BR_MSP};
369//              int Events[NUM_EVENTS] = {PAPI_TOT_CYC, PAPI_L1_DCM};
370//              int Events[NUM_EVENTS] = {PAPI_L2_DCM};
371                int cal_size = 1000;
372                code_clocker = new CC(Events,NUM_EVENTS,cal_size);
373                code_clocker->cc_set_cmd(cmdline);
374        #endif
375
376        // Read the entire file into a memory buffer   
377        FILE * src_file;
378        struct stat fileinfo;
379        int src_filesize;
380
381        // open file and fstat 
382        src_file = fopen ( src_filename , "rb" );
383        if (!src_file) {
384                fprintf(stderr, "Cannot open '%s'. Terminating the process ...\n", src_filename); 
385                exit(-1);
386        }
387        if(fstat(fileno(src_file), &fileinfo)!=0) {
388                fprintf(stderr, "Cannot fstat '%s'. Terminating the process ...\n", src_filename); 
389                exit(-1);
390        }
391       
392        src_filesize = fileinfo.st_size;
393       
394        // close file
395        fclose (src_file);
396       
397        for (int run = 0; run < REPEAT_RUNS; run++) {
398
399                #ifdef PAPI
400                        code_clocker->cc_start_interval();
401                #endif
402
403                parser = Parser_Interface::ParserFactory(src_filename);
404       
405                /*             
406                if (!parser->has_ByteOrderMark()) printf("No ");
407                printf("Byte Order Mark found.\n");
408       
409                if (parser->get_version() == XML_1_0) printf("XML version 1.0 declared.\n");
410                else if (parser->get_version() == XML_1_1) printf("XML version 1.1 declared.\n");
411                else printf ("XML version 1.0 implied by default.\n");
412                if (parser->has_EncodingDecl()) {
413                        printf("XML encoding declared:  %s\n", parser->get_Encoding());
414                }
415                if (parser->standalone_status() == Standalone_yes)
416                        printf("XML standalone = yes declared.\n");
417                else if (parser->standalone_status() == Standalone_no)
418                        printf("XML standalone = no declared.\n");
419                else printf ("XML standalone = no by default.\n");
420                */
421
422//              #ifdef PAPI
423//                      code_clocker->cc_start_interval();
424//              #endif
425               
426                parser->Parse_Prolog();
427                parser->Parse_DocumentContent();
428
429//              #ifdef PAPI
430//                      int elems = src_filesize;
431//                      code_clocker->cc_end_interval(elems);
432//              #endif
433
434                parser->~Parser_Interface();
435                printf("Run %i complete.\n", run);
436
437        }
438
439        #ifdef PAPI
440       
441        #if defined(CODE_CLOCKING) and (CODE_CLOCKING == CHARSET_VALIDATION)
442                code_clocker->cc_set_param("CODE_CLOCKING", "CHARSET_VALIDATION");
443        #endif 
444        #if defined(CODE_CLOCKING) and (CODE_CLOCKING == WS_CONTROL)
445                code_clocker->cc_set_param("CODE_CLOCKING", "WS_CONTROL");
446        #endif
447        #if defined(CODE_CLOCKING) and (CODE_CLOCKING == MARKUP_STREAMS)
448                code_clocker->cc_set_param("CODE_CLOCKING", "MARKUP_STREAMS");
449        #endif
450        #if defined(CODE_CLOCKING) and (CODE_CLOCKING == BITLEX_ALL)
451                code_clocker->cc_set_param("CODE_CLOCKING", "BITLEX_ALL");
452        #endif
453        #if defined(CODE_CLOCKING) and (CODE_CLOCKING == BITPLEX)
454                code_clocker->cc_set_param("CODE_CLOCKING", "BITPLEX");
455        #endif
456        #if defined(CODE_CLOCKING) and (CODE_CLOCKING == BYTEPLEX)
457                code_clocker->cc_set_param("CODE_CLOCKING", "BYTEPLEX");
458        #endif
459        #if defined(CODE_CLOCKING) and (CODE_CLOCKING == ADVANCE_BUFFERS)
460                code_clocker->cc_set_param("CODE_CLOCKING", "ADVANCE_BUFFERS");
461        #endif
462        #if defined(CODE_CLOCKING) and (CODE_CLOCKING == BUFFER_TOTAL)
463                code_clocker->cc_set_param("CODE_CLOCKING", "BUFFER_TOTAL");
464        #endif
465        #if defined(CODE_CLOCKING) and (CODE_CLOCKING == FILE_READING)
466                code_clocker->cc_set_param("CODE_CLOCKING", "FILE_READING");
467        #endif
468        #if defined(OMISSION) and (OMISSION == NONE)
469                code_clocker->cc_set_param("OMISSION", "NONE");
470        #endif
471        #if defined(OMISSION) and (OMISSION == END_TAG_MATCHING)
472                code_clocker->cc_set_param("OMISSION", "END_TAG_MATCHING");
473        #endif
474        #if defined(OMISSION) and (OMISSION == ATTRIBUTE_UNIQUENESS)
475                code_clocker->cc_set_param("OMISSION", "ATTRIBUTE_UNIQUENESS");
476        #endif
477        #if defined(OMISSION) and (OMISSION == NAME_VALIDATION)
478                code_clocker->cc_set_param("OMISSION", "NAME_VALIDATION");
479        #endif
480        #if defined(OMISSION) and (OMISSION == NAME_LOOKUP)
481                code_clocker->cc_set_param("OMISSION", "NAME_LOOKUP");
482        #endif
483        #if defined(VALIDATION_MODE) and (VALIDATION_MODE == ON)
484                code_clocker->cc_set_param("VALIDATION_MODE", "ON");
485        #endif
486        #if defined(VALIDATION_MODE) and (VALIDATION_MODE == OFF)
487                code_clocker->cc_set_param("VALIDATION_MODE", "OFF");
488        #endif
489       
490                code_clocker->cc_write_xml_file();
491                code_clocker->cc_display();
492                delete code_clocker;
493        #endif 
494       
495        print_stats("comment", comment_count, comment_length);
496        print_stats("CDATA section", CDATA_end_count, CDATA_length);
497        print_stats("processing instruction", PI_count, PI_length);
498        print_stats("empty element", empty_elem_count, empty_elem_length);
499        print_stats("start tag", start_tag_count, start_tag_length);
500        printf("%i total attributes\n", total_attribute_count);
501        print_stats("attribute name", total_attribute_count, total_att_name_length);
502        print_stats("attribute value", total_attribute_count, total_att_value_length);
503        print_stats("namespace name", namespace_count, total_namespace_name_length);
504        print_stats("namespace URI", namespace_count, total_namespace_URI_length);
505        print_stats("end tag", end_tag_count, end_tag_length);
506        print_stats("text item", text_item_count, text_item_length);
507        print_stats("reference", reference_count, reference_length);
508        print_stats("error item", error_item_count, error_item_length);
509        printf("Maximum nesting depth = %i\n", max_nesting_depth);
510       
511        return(0);
512}
Note: See TracBrowser for help on using the repository browser.