source: trunk/markup_stats.cxx @ 266

Last change on this file since 266 was 265, checked in by ksherdy, 11 years ago

Call delete over explicit call to destructor.

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