groups.py 5.71 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
#! /usr/bin/python

################################################################################
# A small quick hack script that re-orders and summarizes group definitions.
#
# It uses quite specific logic to get a new grouping but this might serve as
# an example for similar tools.
################################################################################

################################################################################
# 1st parameter = trace to read
################################################################################

from otf import *
import sys



# dict for new groups
newindex= 0

number_def_function= 0
number_def_group= 0
max_def_group= 0


token_to_name= {}
token_to_shortname= {}
token_to_count= {}

token_to_newtoken= {}


### stage 1 handlers ###########################################################

def handleDefFunction_Stage_1( fha, stream, func, name, group, source, kvlist ):
	
#	print ' handleDefFunction: \"%s\", s%u, t%u, nm\"%s\", g%u, src%u' \
#		% (fha,stream, func,name,group,source)

	global number_def_function
	number_def_function += 1

	token_to_count[ group ] += 1


#	# find first part of source file name
	shortname= "empty"
	a= 0
	b= 0

	a= name.find( "[{" )
	if -1 != a:
		b= name.find( "}", a+2 )
		if -1 != b :

			c= name.find( "_", a+2, b )
			if -1 != c :
				b= c

			c= name.find( ".", a+2, b )
			if -1 != c :
				b= c

			c= name.find( " ", a+2, b )
			if -1 != c :
				b= c

			shortname= name[a+2:b]
	token_to_shortname[ group ]= shortname

	return OTF_RETURN_OK


def handleDefFunctionGroup_Stage_1( fha, stream, group, name, kvlist ):
	
#	print ' handleDefFunctionGroup: \"%s\", s%u, t%u, nm\"%s\"' \
#		% (fha,stream,group,name)

	global number_def_group
	number_def_group += 1

	global max_def_group
	max_def_group= max_def_group if ( max_def_group > group ) else group

	token_to_name[ group ]= name

	global token_to_count
	token_to_count[ group ]= 0

	return OTF_RETURN_OK


### stage 2 handlers ###########################################################


def handleDefFunction_Stage_2( fha, stream, func, name, group, source, kvlist ):
	
	OTF_Writer_writeDefFunction( fha, stream, func, name, token_to_newtoken[ group ], source )

	return OTF_RETURN_OK


def handleDefFunctionGroup_Stage_2( fha, stream, group, name, kvlist ):

	return OTF_RETURN_OK


### main #######################################################################


if __name__ == '__main__':
	
	manager= OTF_FileManager_open( 100 )

	inname= sys.argv[1]
	print "read ", inname

	outname= OTF_stripFilename( inname ) + "_new"
	print "write ", outname

	reader= OTF_Reader_open( inname, manager )

	handlers= OTF_HandlerArray_open()


	### stage 1 - create group mapping #########################################


	OTF_HandlerArray_setHandler( handlers, handleDefFunction_Stage_1, OTF_DEFFUNCTION_RECORD )
	OTF_HandlerArray_setHandler( handlers, handleDefFunctionGroup_Stage_1, OTF_DEFFUNCTIONGROUP_RECORD )


	num= OTF_Reader_readDefinitions( reader, handlers )

	print "stage 1: read ", num, " records"
	print "number of function definitions ", number_def_function
	print "number of group definitions", number_def_group
	print "len( token_to_name ) ", len( token_to_name )
	print "len( token_to_count ) ", len( token_to_count )
	print "\n"

#	print "list all:"
#	for k, c in token_to_count.iteritems() :
#		print k, " # ", c, " name ", token_to_name[ k ], " shortname ", token_to_shortname[ k ]

	### compute mapping token_to_newtoken ######################################

	writer= OTF_Writer_open( outname, 0, manager )
	max_def_group += 1

	shortname_to_count= {}

	print "list keep:"
	for k, c in token_to_count.iteritems() :
	
		if c >= 5 :
			print "keep ", k, " # ", c, " name ", token_to_name[ k ]
			OTF_Writer_writeDefFunctionGroup( writer, 0, k,  token_to_name[ k ] )
		else :
			print "k= ", k
			print "token_to_shortname[ k ]", token_to_shortname[ k ]
			shortname_to_count[ token_to_shortname[ k ] ]= 0

	# sum over equal short names
	for k, c in token_to_count.iteritems() :
	
		shortname_to_count[ token_to_shortname[ k ] ] += c

#	for k, c in token_to_shortname.iteritems() :
#		print "    ", k, " # ", c
		
		
	shortname_to_newtoken= {}

	print "list new:"
	for k, c in shortname_to_count.iteritems() :

		if c >= 5 :
			OTF_Writer_writeDefFunctionGroup( writer, 0, max_def_group, k )
			shortname_to_newtoken[ k ]= max_def_group
			print "    new token ", max_def_group, ":", k, " # ", c
			max_def_group += 1

	# define "misc" group
	k= "misc"
	OTF_Writer_writeDefFunctionGroup( writer, 0, max_def_group, k )
	shortname_to_newtoken[ k ]= max_def_group
	print "    new token ", max_def_group, ":", k, " # x"
	max_def_group += 1


	token_to_newtoken= {}

	for k, v in token_to_shortname.iteritems() :

		if token_to_count[ k ] >= 5 :
		
			token_to_newtoken[ k ]= k
		
		else :

			if shortname_to_newtoken.has_key( v ) :

				token_to_newtoken[ k ]= shortname_to_newtoken[ v ];
			else :
				token_to_newtoken[ k ]= shortname_to_newtoken[ "misc" ];
			

#	print "\n mapping \n\n"
#	for t, n in token_to_newtoken.iteritems() :
#		print "   ", t, " --> ", n
	

	### stage 2 - write new def file ###########################################

	# set copy handlers to all but FunctionDefs and FunctionGroupDefs
	OTF_HandlerArray_getCopyHandler( handlers, writer )
	OTF_HandlerArray_setHandler( handlers, handleDefFunction_Stage_2, OTF_DEFFUNCTION_RECORD )
	OTF_HandlerArray_setHandler( handlers, handleDefFunctionGroup_Stage_2, OTF_DEFFUNCTIONGROUP_RECORD )

	OTF_Reader_close( reader )
	reader= OTF_Reader_open( inname, manager )

	num= OTF_Reader_readDefinitions( reader, handlers )

	print "stage 2: read ", num, " records"

	OTF_HandlerArray_close( handlers )
	OTF_Writer_close( writer )
	OTF_Reader_close( reader )
	OTF_FileManager_close( manager )