Skip to content

Commit

Permalink
Change --times console reporting to be based on Activity (#14470)
Browse files Browse the repository at this point in the history
### Changing --times reporting of compiler timing to be based on the Activity module

* Console table layout for --times

* Supporting proper finish of statistic measurement, to enable multiple runs in the same process
  This is especially needed for:
  - Test suites
  - Hosted scenarios

* Making console table markdown friendly (to be directly copy-pastable)
  • Loading branch information
T-Gro authored Jan 2, 2023
1 parent 1821ffc commit 5f6600c
Show file tree
Hide file tree
Showing 14 changed files with 274 additions and 253 deletions.
88 changes: 39 additions & 49 deletions src/Compiler/AbstractIL/ilwrite.fs
Original file line number Diff line number Diff line change
Expand Up @@ -502,9 +502,7 @@ type cenv =

emitTailcalls: bool

deterministic: bool

showTimes: bool
deterministic: bool

desiredMetadataVersion: ILVersionInfo

Expand Down Expand Up @@ -3020,22 +3018,22 @@ let GenModule (cenv : cenv) (modul: ILModuleDef) =
let midx = AddUnsharedRow cenv TableNames.Module (GetModuleAsRow cenv modul)
List.iter (GenResourcePass3 cenv) (modul.Resources.AsList())
let tdefs = destTypeDefsWithGlobalFunctionsFirst cenv.ilg modul.TypeDefs
reportTime cenv.showTimes "Module Generation Preparation"
reportTime "Module Generation Preparation"
GenTypeDefsPass1 [] cenv tdefs
reportTime cenv.showTimes "Module Generation Pass 1"
reportTime "Module Generation Pass 1"
GenTypeDefsPass2 0 [] cenv tdefs
reportTime cenv.showTimes "Module Generation Pass 2"
reportTime "Module Generation Pass 2"
(match modul.Manifest with None -> () | Some m -> GenManifestPass3 cenv m)
GenTypeDefsPass3 [] cenv tdefs
reportTime cenv.showTimes "Module Generation Pass 3"
reportTime "Module Generation Pass 3"
GenCustomAttrsPass3Or4 cenv (hca_Module, midx) modul.CustomAttrs
// GenericParam is the only sorted table indexed by Columns in other tables (GenericParamConstraint\CustomAttributes).
// Hence we need to sort it before we emit any entries in GenericParamConstraint\CustomAttributes that are attached to generic params.
// Note this mutates the rows in a table. 'SetRowsOfTable' clears
// the key --> index map since it is no longer valid
cenv.GetTable(TableNames.GenericParam).SetRowsOfSharedTable (SortTableRows TableNames.GenericParam (cenv.GetTable(TableNames.GenericParam).GenericRowsOfTable))
GenTypeDefsPass4 [] cenv tdefs
reportTime cenv.showTimes "Module Generation Pass 4"
reportTime "Module Generation Pass 4"

/// Arbitrary value
[<Literal>]
Expand All @@ -3053,8 +3051,7 @@ let generateIL (
generatePdb,
ilg: ILGlobals,
emitTailcalls,
deterministic,
showTimes,
deterministic,
referenceAssemblyOnly,
referenceAssemblyAttribOpt: ILAttribute option,
allGivenSources,
Expand Down Expand Up @@ -3095,8 +3092,7 @@ let generateIL (
MetadataTable.Unshared (MetadataTable<UnsharedRow>.New ("row table "+string i, EqualityComparer.Default)))
use cenv =
{ emitTailcalls=emitTailcalls
deterministic = deterministic
showTimes=showTimes
deterministic = deterministic
ilg = ilg
desiredMetadataVersion=desiredMetadataVersion
requiredDataFixups= requiredDataFixups
Expand Down Expand Up @@ -3180,7 +3176,7 @@ let generateIL (
EventTokenMap = (fun t edef ->
let tidx = idxForNextedTypeDef t
getUncodedToken TableNames.Event (cenv.eventDefs.GetTableEntry (EventKey (tidx, edef.Name)))) }
reportTime cenv.showTimes "Finalize Module Generation Results"
reportTime "Finalize Module Generation Results"
// New return the results
let data = cenv.data.AsMemory().ToArray()
let resources = cenv.resources.AsMemory().ToArray()
Expand Down Expand Up @@ -3214,8 +3210,7 @@ let writeILMetadataAndCode (
desiredMetadataVersion,
ilg,
emitTailcalls,
deterministic,
showTimes,
deterministic,
referenceAssemblyOnly,
referenceAssemblyAttribOpt,
allGivenSources,
Expand All @@ -3237,16 +3232,15 @@ let writeILMetadataAndCode (
generatePdb,
ilg,
emitTailcalls,
deterministic,
showTimes,
deterministic,
referenceAssemblyOnly,
referenceAssemblyAttribOpt,
allGivenSources,
modul,
cilStartAddress,
normalizeAssemblyRefs)

reportTime showTimes "Generated Tables and Code"
reportTime "Generated Tables and Code"
let tableSize (tab: TableName) = tables[tab.Index].Count

// Now place the code
Expand Down Expand Up @@ -3318,7 +3312,7 @@ let writeILMetadataAndCode (
(if tableSize TableNames.GenericParamConstraint > 0 then 0x00001000 else 0x00000000) |||
0x00000200

reportTime showTimes "Layout Header of Tables"
reportTime "Layout Header of Tables"

let guidAddress n = (if n = 0 then 0 else (n - 1) * 0x10 + 0x01)

Expand Down Expand Up @@ -3362,7 +3356,7 @@ let writeILMetadataAndCode (
if n >= blobAddressTable.Length then failwith "blob index out of range"
blobAddressTable[n]

reportTime showTimes "Build String/Blob Address Tables"
reportTime "Build String/Blob Address Tables"

let sortedTables =
Array.init 64 (fun i ->
Expand All @@ -3371,7 +3365,7 @@ let writeILMetadataAndCode (
let rows = tab.GenericRowsOfTable
if TableRequiresSorting tabName then SortTableRows tabName rows else rows)

reportTime showTimes "Sort Tables"
reportTime "Sort Tables"

let codedTables =

Expand Down Expand Up @@ -3486,7 +3480,7 @@ let writeILMetadataAndCode (
tablesBuf.EmitInt32 rows.Length


reportTime showTimes "Write Header of tablebuf"
reportTime "Write Header of tablebuf"

// The tables themselves
for rows in sortedTables do
Expand Down Expand Up @@ -3521,7 +3515,7 @@ let writeILMetadataAndCode (

tablesBuf.AsMemory().ToArray()

reportTime showTimes "Write Tables to tablebuf"
reportTime "Write Tables to tablebuf"

let tablesStreamUnpaddedSize = codedTables.Length
// QUERY: extra 4 empty bytes in array.exe - why? Include some extra padding after
Expand All @@ -3538,7 +3532,7 @@ let writeILMetadataAndCode (
let blobsChunk, _next = chunk blobsStreamPaddedSize next
let blobsStreamPadding = blobsChunk.size - blobsStreamUnpaddedSize

reportTime showTimes "Layout Metadata"
reportTime "Layout Metadata"

let metadata, guidStart =
use mdbuf = ByteBuffer.Create(MetadataCapacity, useArrayPool = true)
Expand Down Expand Up @@ -3573,20 +3567,20 @@ let writeILMetadataAndCode (
mdbuf.EmitInt32 blobsChunk.size
mdbuf.EmitIntsAsBytes [| 0x23; 0x42; 0x6c; 0x6f; 0x62; 0x00; 0x00; 0x00; (* #Blob000 *)|]

reportTime showTimes "Write Metadata Header"
reportTime "Write Metadata Header"
// Now the coded tables themselves
mdbuf.EmitBytes codedTables
for i = 1 to tablesStreamPadding do
mdbuf.EmitIntAsByte 0x00
reportTime showTimes "Write Metadata Tables"
reportTime "Write Metadata Tables"

// The string stream
mdbuf.EmitByte 0x00uy
for s in strings do
mdbuf.EmitBytes s
for i = 1 to stringsStreamPadding do
mdbuf.EmitIntAsByte 0x00
reportTime showTimes "Write Metadata Strings"
reportTime "Write Metadata Strings"
// The user string stream
mdbuf.EmitByte 0x00uy
for s in userStrings do
Expand All @@ -3596,7 +3590,7 @@ let writeILMetadataAndCode (
for i = 1 to userStringsStreamPadding do
mdbuf.EmitIntAsByte 0x00

reportTime showTimes "Write Metadata User Strings"
reportTime "Write Metadata User Strings"
// The GUID stream
let guidStart = mdbuf.Position
Array.iter mdbuf.EmitBytes guids
Expand All @@ -3608,7 +3602,7 @@ let writeILMetadataAndCode (
mdbuf.EmitBytes s
for i = 1 to blobsStreamPadding do
mdbuf.EmitIntAsByte 0x00
reportTime showTimes "Write Blob Stream"
reportTime "Write Blob Stream"
// Done - close the buffer and return the result.
mdbuf.AsMemory().ToArray(), guidStart

Expand All @@ -3624,7 +3618,7 @@ let writeILMetadataAndCode (
let token = getUncodedToken TableNames.UserStrings (userStringAddress userStringIndex)
if (Bytes.get code (locInCode-1) <> i_ldstr) then failwith "strings-in-code fixup: not at ldstr instruction!"
applyFixup32 code locInCode token
reportTime showTimes "Fixup Metadata"
reportTime "Fixup Metadata"

entryPointToken, code, codePadding, metadata, data, resources, requiredDataFixups.Value, pdbData, mappings, guidStart

Expand Down Expand Up @@ -3687,8 +3681,7 @@ let writeDirectory os dict =
let writeBytes (os: BinaryWriter) (chunk: byte[]) = os.Write(chunk, 0, chunk.Length)

let writePdb (
dumpDebugInfo,
showTimes,
dumpDebugInfo,
embeddedPDB,
pdbfile,
outfile,
Expand Down Expand Up @@ -3721,7 +3714,7 @@ let writePdb (
s.SignStream fs
with exn ->
failwith ($"Warning: A call to SignFile failed ({exn.Message})")
reportTime showTimes "Signing Image"
reportTime "Signing Image"

// Now we've done the bulk of the binary, do the PDB file and fixup the binary.
match pdbfile with
Expand Down Expand Up @@ -3751,7 +3744,7 @@ let writePdb (
stream.WriteTo fs
getInfoForPortablePdb contentId pdbfile pathMap debugDataChunk debugDeterministicPdbChunk debugChecksumPdbChunk algorithmName checkSum embeddedPDB deterministic
| None -> [| |]
reportTime showTimes "Generate PDB Info"
reportTime "Generate PDB Info"

// Now we have the debug data we can go back and fill in the debug directory in the image
use fs2 = reopenOutput()
Expand All @@ -3776,14 +3769,15 @@ let writePdb (
os2.BaseStream.Seek (int64 (textV2P i.iddChunk.addr), SeekOrigin.Begin) |> ignore
if i.iddChunk.size < i.iddData.Length then failwith "Debug data area is not big enough. Debug info may not be usable"
writeBytes os2 i.iddData
reportTime showTimes "Finalize PDB"
reportTime "Finalize PDB"
signImage ()
os2.Dispose()
with exn ->
failwith ("Error while writing debug directory entry: " + exn.Message)
(try os2.Dispose(); FileSystem.FileDeleteShim outfile with _ -> ())
reraise()


reportTime "Finish"
pdbBytes

type options =
Expand All @@ -3799,8 +3793,7 @@ type options =
checksumAlgorithm: HashAlgorithm
signer: ILStrongNameSigner option
emitTailcalls: bool
deterministic: bool
showTimes: bool
deterministic: bool
dumpDebugInfo: bool
referenceAssemblyOnly: bool
referenceAssemblyAttribOpt: ILAttribute option
Expand All @@ -3811,7 +3804,7 @@ let writeBinaryAux (stream: Stream, options: options, modul, normalizeAssemblyRe
// Store the public key from the signer into the manifest. This means it will be written
// to the binary and also acts as an indicator to leave space for delay sign

reportTime options.showTimes "Write Started"
reportTime "Write Started"
let isDll = modul.IsDLL
let ilg = options.ilg

Expand Down Expand Up @@ -3925,8 +3918,7 @@ let writeBinaryAux (stream: Stream, options: options, modul, normalizeAssemblyRe
desiredMetadataVersion,
ilg,
options.emitTailcalls,
options.deterministic,
options.showTimes,
options.deterministic,
options.referenceAssemblyOnly,
options.referenceAssemblyAttribOpt,
options.allGivenSources,
Expand All @@ -3935,7 +3927,7 @@ let writeBinaryAux (stream: Stream, options: options, modul, normalizeAssemblyRe
normalizeAssemblyRefs
)

reportTime options.showTimes "Generated IL and metadata"
reportTime "Generated IL and metadata"
let _codeChunk, next = chunk code.Length next
let _codePaddingChunk, next = chunk codePadding.Length next

Expand Down Expand Up @@ -3968,7 +3960,7 @@ let writeBinaryAux (stream: Stream, options: options, modul, normalizeAssemblyRe
match options.pdbfile, options.portablePDB with
| Some _, true ->
let pdbInfo =
generatePortablePdb options.embedAllSource options.embedSourceList options.sourceLink options.checksumAlgorithm options.showTimes pdbData options.pathMap
generatePortablePdb options.embedAllSource options.embedSourceList options.sourceLink options.checksumAlgorithm pdbData options.pathMap

if options.embeddedPDB then
let (uncompressedLength, contentId, stream, algorithmName, checkSum) = pdbInfo
Expand Down Expand Up @@ -4094,7 +4086,7 @@ let writeBinaryAux (stream: Stream, options: options, modul, normalizeAssemblyRe
let imageEndSectionPhysLoc = nextPhys
let imageEndAddr = next

reportTime options.showTimes "Layout image"
reportTime "Layout image"

let write p (os: BinaryWriter) chunkName chunk =
match p with
Expand Down Expand Up @@ -4501,7 +4493,7 @@ let writeBinaryAux (stream: Stream, options: options, modul, normalizeAssemblyRe

pdbData, pdbInfoOpt, debugDirectoryChunk, debugDataChunk, debugChecksumPdbChunk, debugEmbeddedPdbChunk, debugDeterministicPdbChunk, textV2P, mappings

reportTime options.showTimes "Writing Image"
reportTime "Writing Image"
pdbData, pdbInfoOpt, debugDirectoryChunk, debugDataChunk, debugChecksumPdbChunk, debugEmbeddedPdbChunk, debugDeterministicPdbChunk, textV2P, mappings

let writeBinaryFiles (options: options, modul, normalizeAssemblyRefs) =
Expand Down Expand Up @@ -4529,8 +4521,7 @@ let writeBinaryFiles (options: options, modul, normalizeAssemblyRefs) =
let reopenOutput () =
FileSystem.OpenFileForWriteShim(options.outfile, FileMode.Open, FileAccess.ReadWrite, FileShare.Read)

writePdb (options.dumpDebugInfo,
options.showTimes,
writePdb (options.dumpDebugInfo,
options.embeddedPDB,
options.pdbfile,
options.outfile,
Expand Down Expand Up @@ -4562,8 +4553,7 @@ let writeBinaryInMemory (options: options, modul, normalizeAssemblyRefs) =
stream

let pdbBytes =
writePdb (options.dumpDebugInfo,
options.showTimes,
writePdb (options.dumpDebugInfo,
options.embeddedPDB,
options.pdbfile,
options.outfile,
Expand Down
1 change: 0 additions & 1 deletion src/Compiler/AbstractIL/ilwrite.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ type options =
signer: ILStrongNameSigner option
emitTailcalls: bool
deterministic: bool
showTimes: bool
dumpDebugInfo: bool
referenceAssemblyOnly: bool
referenceAssemblyAttribOpt: ILAttribute option
Expand Down
14 changes: 6 additions & 8 deletions src/Compiler/AbstractIL/ilwritepdb.fs
Original file line number Diff line number Diff line change
Expand Up @@ -316,10 +316,10 @@ let pdbGetDebugInfo
let getDebugFileName outfile =
(FileSystemUtils.chopExtension outfile) + ".pdb"

let sortMethods showTimes info =
reportTime showTimes (sprintf "PDB: Defined %d documents" info.Documents.Length)
let sortMethods info =
reportTime (sprintf "PDB: Defined %d documents" info.Documents.Length)
Array.sortInPlaceBy (fun x -> x.MethToken) info.Methods
reportTime showTimes (sprintf "PDB: Sorted %d methods" info.Methods.Length)
reportTime (sprintf "PDB: Sorted %d methods" info.Methods.Length)
()

let getRowCounts tableRowCounts =
Expand All @@ -345,7 +345,6 @@ type PortablePdbGenerator
embedSourceList: string list,
sourceLink: string,
checksumAlgorithm,
showTimes,
info: PdbData,
pathMap: PathMap
) =
Expand Down Expand Up @@ -784,7 +783,7 @@ type PortablePdbGenerator
| Some scope -> writeMethodScopes minfo.MethToken scope

member _.Emit() =
sortMethods showTimes info
sortMethods info
metadata.SetCapacity(TableIndex.MethodDebugInformation, info.Methods.Length)

defineModuleImportScope ()
Expand Down Expand Up @@ -823,20 +822,19 @@ type PortablePdbGenerator
let contentId = serializer.Serialize blobBuilder
let portablePdbStream = new MemoryStream()
blobBuilder.WriteContentTo portablePdbStream
reportTime showTimes "PDB: Created"
reportTime "PDB: Created"
(portablePdbStream.Length, contentId, portablePdbStream, algorithmName, contentHash)

let generatePortablePdb
(embedAllSource: bool)
(embedSourceList: string list)
(sourceLink: string)
checksumAlgorithm
showTimes
(info: PdbData)
(pathMap: PathMap)
=
let generator =
PortablePdbGenerator(embedAllSource, embedSourceList, sourceLink, checksumAlgorithm, showTimes, info, pathMap)
PortablePdbGenerator(embedAllSource, embedSourceList, sourceLink, checksumAlgorithm, info, pathMap)

generator.Emit()

Expand Down
Loading

0 comments on commit 5f6600c

Please sign in to comment.