@@ -78,6 +78,7 @@ public static class NGramStats {
7878		public  final  long  n ;
7979		public  final  long  cumTimeNanos ;
8080		public  final  double  m2 ;
81+ 		public  final  HashMap <String , Double > meta ;
8182
8283		public  static  <T > Comparator <NGramBuilder .NGramEntry <T , NGramStats >> getComparator () {
8384			return  Comparator .comparingLong (entry  -> entry .getCumStats ().cumTimeNanos );
@@ -95,13 +96,25 @@ public static NGramStats merge(NGramStats stats1, NGramStats stats2) {
9596
9697			double  newM2  = stats1 .m2  + stats2 .m2  + delta  * delta  * stats1 .n  * stats2 .n  / (double )newN ;
9798
98- 			return  new  NGramStats (newN , cumTimeNanos , newM2 );
99+ 			HashMap <String , Double > cpy  = null ;
100+ 
101+ 			if  (stats1 .meta  != null ) {
102+ 				cpy  = new  HashMap <>(stats1 .meta );
103+ 				final  HashMap <String , Double > mCpy  = cpy ;
104+ 				if  (stats2 .meta  != null )
105+ 					stats2 .meta .forEach ((key , value ) -> mCpy .merge (key , value , Double ::sum ));
106+ 			} else  if  (stats2 .meta  != null ) {
107+ 				cpy  = new  HashMap <>(stats2 .meta );
108+ 			}
109+ 
110+ 			return  new  NGramStats (newN , cumTimeNanos , newM2 , cpy );
99111		}
100112
101- 		public  NGramStats (final  long  n , final  long  cumTimeNanos , final  double  m2 ) {
113+ 		public  NGramStats (final  long  n , final  long  cumTimeNanos , final  double  m2 ,  HashMap < String ,  Double >  meta ) {
102114			this .n  = n ;
103115			this .cumTimeNanos  = cumTimeNanos ;
104116			this .m2  = m2 ;
117+ 			this .meta  = meta ;
105118		}
106119
107120		public  double  getTimeVariance () {
@@ -111,13 +124,19 @@ public double getTimeVariance() {
111124		public  String  toString () {
112125			return  String .format (Locale .US , "%.5f" , (cumTimeNanos  / 1000000000d ));
113126		}
127+ 
128+ 		public  HashMap <String , Double > getMeta () {
129+ 			return  meta ;
130+ 		}
114131	}
115132
116133	public  static  class  LineageNGramExtension  {
117134		private  String  _datatype ;
118135		private  String  _valuetype ;
119136		private  long  _execNanos ;
120137
138+ 		private  HashMap <String , Double > _meta ;
139+ 
121140		public  void  setDataType (String  dataType ) {
122141			_datatype  = dataType ;
123142		}
@@ -141,6 +160,18 @@ public void setExecNanos(long nanos) {
141160		public  long  getExecNanos () {
142161			return  _execNanos ;
143162		}
163+ 
164+ 		public  void  setMeta (String  key , Double  value ) {
165+ 			if  (_meta  == null )
166+ 				_meta  = new  HashMap <>();
167+ 			_meta .put (key , value );
168+ 		}
169+ 
170+ 		public  Object  getMeta (String  key ) {
171+ 			if  (_meta  == null )
172+ 				return  null ;
173+ 			return  _meta .get (key );
174+ 		}
144175	}
145176
146177	private  static  long  compileStartTime  = 0 ;
@@ -508,10 +539,10 @@ private static void addLineagePaths(LineageItem li, ArrayList<Entry<LineageItem,
508539			clearNGramRecording ();
509540			// We then record a new n-gram with all the LineageItems of the current lineage path 
510541			Entry <LineageItem , LineageNGramExtension > currentEntry  = currentPath .get (currentPath .size ()-1 );
511- 			matchingBuilder .append (LineageItemUtils .explainLineageAsInstruction (currentEntry .getKey (), currentEntry .getValue ()) + (indexes .size () > 0  ? ("["  + indexes .get (currentPath .size ()-2 ) + "]" ) : "" ), new  NGramStats (1 , currentEntry .getValue () != null  ? currentEntry .getValue ().getExecNanos () : 0 , 0 ));
542+ 			matchingBuilder .append (LineageItemUtils .explainLineageAsInstruction (currentEntry .getKey (), currentEntry .getValue ()) + (indexes .size () > 0  ? ("["  + indexes .get (currentPath .size ()-2 ) + "]" ) : "" ), new  NGramStats (1 , currentEntry .getValue () != null  ? currentEntry .getValue ().getExecNanos () : 0 , 0 ,  currentEntry . getValue () !=  null  ?  currentEntry . getValue (). _meta  :  null ));
512543			for  (int  i  = currentPath .size ()-2 ; i  >= 0 ; i --) {
513544				currentEntry  = currentPath .get (i );
514- 				matchingBuilder .append (LineageItemUtils .explainLineageAsInstruction (currentEntry .getKey (), currentEntry .getValue ()) + (i  > 0  ? ("["  + indexes .get (i -1 ) + "]" ) : "" ), new  NGramStats (1 , currentEntry .getValue () != null  ? currentEntry .getValue ().getExecNanos () : 0 , 0 ));
545+ 				matchingBuilder .append (LineageItemUtils .explainLineageAsInstruction (currentEntry .getKey (), currentEntry .getValue ()) + (i  > 0  ? ("["  + indexes .get (i -1 ) + "]" ) : "" ), new  NGramStats (1 , currentEntry .getValue () != null  ? currentEntry .getValue ().getExecNanos () : 0 , 0 ,  currentEntry . getValue () !=  null  ?  currentEntry . getValue (). _meta  :  null ));
515546			}
516547		}
517548
@@ -538,7 +569,7 @@ public static void maintainNGrams(String instName, long timeNanos) {
538569		});
539570
540571		for  (int  i  = 0 ; i  < tmp .length ; i ++)
541- 			tmp [i ].append (instName , new  NGramStats (1 , timeNanos , 0 ));
572+ 			tmp [i ].append (instName , new  NGramStats (1 , timeNanos , 0 ,  null ));
542573	}
543574
544575	@ SuppressWarnings ("unchecked" )
@@ -604,6 +635,8 @@ public static String nGramToCSV(final NGramBuilder<String, NGramStats> mbuilder)
604635			colList .add ("Col"  + (j  + 1 ) + "::Mean(Time[s])" );
605636		for  (int  j  = 0 ; j  < mbuilder .getSize (); j ++)
606637			colList .add ("Col"  + (j  + 1 ) + "::StdDev(Time[s])/Col"  + (j  + 1 ) + "::Mean(Time[s])" );
638+ 		for  (int  j  = 0 ; j  < mbuilder .getSize (); j ++)
639+ 			colList .add ("Col"  + (j  + 1 ) + "_Meta" );
607640
608641		colList .add ("Count" );
609642
@@ -620,6 +653,18 @@ public static String nGramToCSV(final NGramBuilder<String, NGramStats> mbuilder)
620653			} else  {
621654				builder .append (stdDevs );
622655			}
656+ 			builder .append ("," );
657+ 			if  (e .getCumStats ().getMeta () != null ) {
658+ 				boolean  first  = true ;
659+ 				for  (Entry <String , Double > metaData  : e .getCumStats ().getMeta ().entrySet ()) {
660+ 					if  (first )
661+ 						first  = false ;
662+ 					else 
663+ 						builder .append ("&" );
664+ 					if  (metaData .getValue () != null )
665+ 						builder .append (metaData .getKey () + ":"  + metaData .getValue ());
666+ 				}
667+ 			}
623668			return  builder .toString ();
624669		});
625670	}
0 commit comments