linuxboot / fiano

Go-based tools for modifying UEFI firmware
BSD 3-Clause "New" or "Revised" License
302 stars 49 forks source link

`uefi.Parse` works very slowly due to LZMA #310

Closed xaionaro closed 3 years ago

xaionaro commented 4 years ago

Hello!

I'm trying to check if my tool (which analyzed damaged images) works correctly with any damaged images. And therefore in an unit-test I just try to sequentially "damage" every byte of an image and check if everything works as expected. But preliminary estimation showed that this experiment would require 94 hours on my laptop. And cpuprofile shows, that 96% of time is utlized in github.com/ulikunitz/xz/lzma.(*decoder).decompress. So I was wondering: is there a way to disable the decompression (or may be cache the results)?

(pprof) top100
Showing nodes accounting for 32.74s, 95.68% of 34.22s total
Dropped 171 nodes (cum <= 0.17s)
      flat  flat%   sum%        cum   cum%
    11.90s 34.77% 34.77%     14.03s 41.00%  github.com/ulikunitz/xz/lzma.(*rangeDecoder).DecodeBit
     2.68s  7.83% 42.61%      8.86s 25.89%  github.com/ulikunitz/xz/lzma.(*literalCodec).Decode
     1.80s  5.26% 47.87%      5.64s 16.48%  github.com/ulikunitz/xz/lzma.(*treeCodec).Decode
     1.60s  4.68% 52.54%      1.60s  4.68%  runtime.memmove
     1.45s  4.24% 56.78%      3.47s 10.14%  runtime.mallocgc
     1.15s  3.36% 60.14%     28.48s 83.23%  github.com/ulikunitz/xz/lzma.(*decoder).readOp
        1s  2.92% 63.06%      1.17s  3.42%  github.com/ulikunitz/xz/lzma.(*rangeDecoder).DirectDecodeBit
        1s  2.92% 65.98%      2.41s  7.04%  github.com/ulikunitz/xz/lzma.(*treeReverseCodec).Decode
     0.79s  2.31% 68.29%      0.79s  2.31%  runtime.memclrNoHeapPointers
     0.77s  2.25% 70.54%      3.65s 10.67%  runtime.convT2Inoptr
     0.76s  2.22% 72.76%      1.31s  3.83%  github.com/ulikunitz/xz/lzma.(*rangeDecoder).updateCode
     0.73s  2.13% 74.90%         2s  5.84%  github.com/ulikunitz/xz/lzma.(*buffer).Write
     0.65s  1.90% 76.80%      2.74s  8.01%  github.com/ulikunitz/xz/lzma.(*decoderDict).writeMatch
     0.63s  1.84% 78.64%     11.56s 33.78%  github.com/ulikunitz/xz/lzma.(*decoder).decodeLiteral
     0.56s  1.64% 80.27%      3.75s 10.96%  github.com/ulikunitz/xz/lzma.(*decoder).apply
     0.52s  1.52% 81.79%      1.69s  4.94%  github.com/ulikunitz/xz/lzma.directCodec.Decode
     0.43s  1.26% 83.05%      0.43s  1.26%  github.com/ulikunitz/xz/lzma.(*prob).inc
     0.39s  1.14% 84.19%      0.39s  1.14%  runtime.nextFreeFast
     0.38s  1.11% 85.30%      0.38s  1.11%  github.com/ulikunitz/xz/lzma.prob.bound
     0.35s  1.02% 86.32%     32.74s 95.68%  github.com/ulikunitz/xz/lzma.(*decoder).decompress
     0.33s  0.96% 87.29%      0.33s  0.96%  runtime.acquirem
     0.31s  0.91% 88.19%      0.31s  0.91%  github.com/ulikunitz/xz/lzma.(*state).states
     0.30s  0.88% 89.07%      0.30s  0.88%  github.com/ulikunitz/xz/lzma.(*buffer).Available
     0.29s  0.85% 89.92%      0.54s  1.58%  bytes.(*Buffer).ReadByte
     0.28s  0.82% 90.74%      7.31s 21.36%  github.com/ulikunitz/xz/lzma.(*distCodec).Decode
     0.26s  0.76% 91.50%      0.38s  1.11%  github.com/ulikunitz/xz/lzma.(*decoderDict).byteAt
     0.25s  0.73% 92.23%      0.25s  0.73%  bytes.(*Buffer).empty
     0.24s   0.7% 92.93%      0.24s   0.7%  github.com/ulikunitz/xz/lzma.(*state).updateStateLiteral
     0.23s  0.67% 93.60%      3.55s 10.37%  github.com/ulikunitz/xz/lzma.(*lengthCodec).Decode
     0.19s  0.56% 94.16%      2.79s  8.15%  github.com/ulikunitz/xz/lzma.(*prob).Decode
     0.18s  0.53% 94.68%      0.18s  0.53%  github.com/ulikunitz/xz/lzma.(*prob).dec
     0.13s  0.38% 95.06%      0.32s  0.94%  github.com/ulikunitz/xz/lzma.(*buffer).WriteByte
     0.13s  0.38% 95.44%      0.45s  1.32%  github.com/ulikunitz/xz/lzma.(*decoderDict).WriteByte
     0.07s   0.2% 95.65%      0.19s  0.56%  github.com/ulikunitz/xz/lzma.(*decoderDict).dictLen
     0.01s 0.029% 95.68%      0.18s  0.53%  runtime.(*mcentral).cacheSpan
         0     0% 95.68%     33.92s 99.12%  experimental/xaionaro/pcr0/pkg/uefi.ParseUEFIFirmwareBytes
         0     0% 95.68%     33.86s 98.95%  experimental/xaionaro/pcr0/pkg/uefi.TestUEFI_GetPCRMeasurements.func1.1
         0     0% 95.68%     32.89s 96.11%  bytes.(*Buffer).ReadFrom
         0     0% 95.68%     33.48s 97.84%  github.com/linuxboot/fiano/pkg/compression.(*LZMA).Decode
         0     0% 95.68%     33.48s 97.84%  github.com/linuxboot/fiano/pkg/compression.(*SystemLZMA).Decode
         0     0% 95.68%     33.92s 99.12%  github.com/linuxboot/fiano/pkg/uefi.NewBIOSRegion
         0     0% 95.68%     33.67s 98.39%  github.com/linuxboot/fiano/pkg/uefi.NewFile
         0     0% 95.68%     33.82s 98.83%  github.com/linuxboot/fiano/pkg/uefi.NewFirmwareVolume
         0     0% 95.68%     33.65s 98.33%  github.com/linuxboot/fiano/pkg/uefi.NewSection
         0     0% 95.68%     33.92s 99.12%  github.com/linuxboot/fiano/pkg/uefi.Parse
         0     0% 95.68%     32.78s 95.79%  github.com/ulikunitz/xz/lzma.(*Reader).Read
         0     0% 95.68%     32.78s 95.79%  github.com/ulikunitz/xz/lzma.(*decoder).Read
         0     0% 95.68%      0.58s  1.69%  github.com/ulikunitz/xz/lzma.NewReader
         0     0% 95.68%      0.58s  1.69%  github.com/ulikunitz/xz/lzma.ReaderConfig.NewReader
         0     0% 95.68%      0.57s  1.67%  github.com/ulikunitz/xz/lzma.newBuffer
         0     0% 95.68%      0.58s  1.69%  github.com/ulikunitz/xz/lzma.newDecoderDict
         0     0% 95.68%     32.90s 96.14%  io/ioutil.ReadAll
         0     0% 95.68%     32.90s 96.14%  io/ioutil.readAll
         0     0% 95.68%      0.22s  0.64%  runtime.(*mcache).nextFree
         0     0% 95.68%      0.18s  0.53%  runtime.(*mcache).refill
         0     0% 95.68%      0.90s  2.63%  runtime.(*mheap).alloc
         0     0% 95.68%      0.83s  2.43%  runtime.largeAlloc
         0     0% 95.68%      0.86s  2.51%  runtime.makeslice
         0     0% 95.68%      0.83s  2.43%  runtime.mallocgc.func1
         0     0% 95.68%      1.08s  3.16%  runtime.systemstack

When I just change (in ./pkg/uefi/section.go):

        if typeSpec.Attributes&uint16(GUIDEDSectionProcessingRequired) != 0 {

to

        if false && typeSpec.Attributes&uint16(GUIDEDSectionProcessingRequired) != 0 {

I receive a speed-up in 9.5 times.

Related: https://github.com/linuxboot/fiano/issues/311 Related: https://github.com/linuxboot/fiano/issues/302

xaionaro commented 3 years ago

Was remediated in https://github.com/linuxboot/fiano/pull/326