Nav apraksta

encode.go 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. // Copyright 2014 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package profile
  15. import (
  16. "errors"
  17. "fmt"
  18. "sort"
  19. )
  20. func (p *Profile) decoder() []decoder {
  21. return profileDecoder
  22. }
  23. // preEncode populates the unexported fields to be used by encode
  24. // (with suffix X) from the corresponding exported fields. The
  25. // exported fields are cleared up to facilitate testing.
  26. func (p *Profile) preEncode() {
  27. strings := make(map[string]int)
  28. addString(strings, "")
  29. for _, st := range p.SampleType {
  30. st.typeX = addString(strings, st.Type)
  31. st.unitX = addString(strings, st.Unit)
  32. }
  33. for _, s := range p.Sample {
  34. s.labelX = nil
  35. var keys []string
  36. for k := range s.Label {
  37. keys = append(keys, k)
  38. }
  39. sort.Strings(keys)
  40. for _, k := range keys {
  41. vs := s.Label[k]
  42. for _, v := range vs {
  43. s.labelX = append(s.labelX,
  44. label{
  45. keyX: addString(strings, k),
  46. strX: addString(strings, v),
  47. },
  48. )
  49. }
  50. }
  51. var numKeys []string
  52. for k := range s.NumLabel {
  53. numKeys = append(numKeys, k)
  54. }
  55. sort.Strings(numKeys)
  56. for _, k := range numKeys {
  57. vs := s.NumLabel[k]
  58. for _, v := range vs {
  59. s.labelX = append(s.labelX,
  60. label{
  61. keyX: addString(strings, k),
  62. numX: v,
  63. },
  64. )
  65. }
  66. }
  67. s.locationIDX = make([]uint64, len(s.Location))
  68. for i, loc := range s.Location {
  69. s.locationIDX[i] = loc.ID
  70. }
  71. }
  72. for _, m := range p.Mapping {
  73. m.fileX = addString(strings, m.File)
  74. m.buildIDX = addString(strings, m.BuildID)
  75. }
  76. for _, l := range p.Location {
  77. for i, ln := range l.Line {
  78. if ln.Function != nil {
  79. l.Line[i].functionIDX = ln.Function.ID
  80. } else {
  81. l.Line[i].functionIDX = 0
  82. }
  83. }
  84. if l.Mapping != nil {
  85. l.mappingIDX = l.Mapping.ID
  86. } else {
  87. l.mappingIDX = 0
  88. }
  89. }
  90. for _, f := range p.Function {
  91. f.nameX = addString(strings, f.Name)
  92. f.systemNameX = addString(strings, f.SystemName)
  93. f.filenameX = addString(strings, f.Filename)
  94. }
  95. p.dropFramesX = addString(strings, p.DropFrames)
  96. p.keepFramesX = addString(strings, p.KeepFrames)
  97. if pt := p.PeriodType; pt != nil {
  98. pt.typeX = addString(strings, pt.Type)
  99. pt.unitX = addString(strings, pt.Unit)
  100. }
  101. p.commentX = nil
  102. for _, c := range p.Comments {
  103. p.commentX = append(p.commentX, addString(strings, c))
  104. }
  105. p.defaultSampleTypeX = addString(strings, p.DefaultSampleType)
  106. p.stringTable = make([]string, len(strings))
  107. for s, i := range strings {
  108. p.stringTable[i] = s
  109. }
  110. }
  111. func (p *Profile) encode(b *buffer) {
  112. for _, x := range p.SampleType {
  113. encodeMessage(b, 1, x)
  114. }
  115. for _, x := range p.Sample {
  116. encodeMessage(b, 2, x)
  117. }
  118. for _, x := range p.Mapping {
  119. encodeMessage(b, 3, x)
  120. }
  121. for _, x := range p.Location {
  122. encodeMessage(b, 4, x)
  123. }
  124. for _, x := range p.Function {
  125. encodeMessage(b, 5, x)
  126. }
  127. encodeStrings(b, 6, p.stringTable)
  128. encodeInt64Opt(b, 7, p.dropFramesX)
  129. encodeInt64Opt(b, 8, p.keepFramesX)
  130. encodeInt64Opt(b, 9, p.TimeNanos)
  131. encodeInt64Opt(b, 10, p.DurationNanos)
  132. if pt := p.PeriodType; pt != nil && (pt.typeX != 0 || pt.unitX != 0) {
  133. encodeMessage(b, 11, p.PeriodType)
  134. }
  135. encodeInt64Opt(b, 12, p.Period)
  136. encodeInt64s(b, 13, p.commentX)
  137. encodeInt64(b, 14, p.defaultSampleTypeX)
  138. }
  139. var profileDecoder = []decoder{
  140. nil, // 0
  141. // repeated ValueType sample_type = 1
  142. func(b *buffer, m message) error {
  143. x := new(ValueType)
  144. pp := m.(*Profile)
  145. pp.SampleType = append(pp.SampleType, x)
  146. return decodeMessage(b, x)
  147. },
  148. // repeated Sample sample = 2
  149. func(b *buffer, m message) error {
  150. x := new(Sample)
  151. pp := m.(*Profile)
  152. pp.Sample = append(pp.Sample, x)
  153. return decodeMessage(b, x)
  154. },
  155. // repeated Mapping mapping = 3
  156. func(b *buffer, m message) error {
  157. x := new(Mapping)
  158. pp := m.(*Profile)
  159. pp.Mapping = append(pp.Mapping, x)
  160. return decodeMessage(b, x)
  161. },
  162. // repeated Location location = 4
  163. func(b *buffer, m message) error {
  164. x := new(Location)
  165. x.Line = make([]Line, 0, 8) // Pre-allocate Line buffer
  166. pp := m.(*Profile)
  167. pp.Location = append(pp.Location, x)
  168. err := decodeMessage(b, x)
  169. var tmp []Line
  170. x.Line = append(tmp, x.Line...) // Shrink to allocated size
  171. return err
  172. },
  173. // repeated Function function = 5
  174. func(b *buffer, m message) error {
  175. x := new(Function)
  176. pp := m.(*Profile)
  177. pp.Function = append(pp.Function, x)
  178. return decodeMessage(b, x)
  179. },
  180. // repeated string string_table = 6
  181. func(b *buffer, m message) error {
  182. err := decodeStrings(b, &m.(*Profile).stringTable)
  183. if err != nil {
  184. return err
  185. }
  186. if *&m.(*Profile).stringTable[0] != "" {
  187. return errors.New("string_table[0] must be ''")
  188. }
  189. return nil
  190. },
  191. // int64 drop_frames = 7
  192. func(b *buffer, m message) error { return decodeInt64(b, &m.(*Profile).dropFramesX) },
  193. // int64 keep_frames = 8
  194. func(b *buffer, m message) error { return decodeInt64(b, &m.(*Profile).keepFramesX) },
  195. // int64 time_nanos = 9
  196. func(b *buffer, m message) error { return decodeInt64(b, &m.(*Profile).TimeNanos) },
  197. // int64 duration_nanos = 10
  198. func(b *buffer, m message) error { return decodeInt64(b, &m.(*Profile).DurationNanos) },
  199. // ValueType period_type = 11
  200. func(b *buffer, m message) error {
  201. x := new(ValueType)
  202. pp := m.(*Profile)
  203. pp.PeriodType = x
  204. return decodeMessage(b, x)
  205. },
  206. // int64 period = 12
  207. func(b *buffer, m message) error { return decodeInt64(b, &m.(*Profile).Period) },
  208. // repeated int64 comment = 13
  209. func(b *buffer, m message) error { return decodeInt64s(b, &m.(*Profile).commentX) },
  210. // int64 defaultSampleType = 14
  211. func(b *buffer, m message) error { return decodeInt64(b, &m.(*Profile).defaultSampleTypeX) },
  212. }
  213. // postDecode takes the unexported fields populated by decode (with
  214. // suffix X) and populates the corresponding exported fields.
  215. // The unexported fields are cleared up to facilitate testing.
  216. func (p *Profile) postDecode() error {
  217. var err error
  218. mappings := make(map[uint64]*Mapping)
  219. for _, m := range p.Mapping {
  220. m.File, err = getString(p.stringTable, &m.fileX, err)
  221. m.BuildID, err = getString(p.stringTable, &m.buildIDX, err)
  222. mappings[m.ID] = m
  223. }
  224. functions := make(map[uint64]*Function)
  225. for _, f := range p.Function {
  226. f.Name, err = getString(p.stringTable, &f.nameX, err)
  227. f.SystemName, err = getString(p.stringTable, &f.systemNameX, err)
  228. f.Filename, err = getString(p.stringTable, &f.filenameX, err)
  229. functions[f.ID] = f
  230. }
  231. locations := make(map[uint64]*Location)
  232. for _, l := range p.Location {
  233. l.Mapping = mappings[l.mappingIDX]
  234. l.mappingIDX = 0
  235. for i, ln := range l.Line {
  236. if id := ln.functionIDX; id != 0 {
  237. l.Line[i].Function = functions[id]
  238. if l.Line[i].Function == nil {
  239. return fmt.Errorf("Function ID %d not found", id)
  240. }
  241. l.Line[i].functionIDX = 0
  242. }
  243. }
  244. locations[l.ID] = l
  245. }
  246. for _, st := range p.SampleType {
  247. st.Type, err = getString(p.stringTable, &st.typeX, err)
  248. st.Unit, err = getString(p.stringTable, &st.unitX, err)
  249. }
  250. for _, s := range p.Sample {
  251. labels := make(map[string][]string)
  252. numLabels := make(map[string][]int64)
  253. for _, l := range s.labelX {
  254. var key, value string
  255. key, err = getString(p.stringTable, &l.keyX, err)
  256. if l.strX != 0 {
  257. value, err = getString(p.stringTable, &l.strX, err)
  258. labels[key] = append(labels[key], value)
  259. } else if l.numX != 0 {
  260. numLabels[key] = append(numLabels[key], l.numX)
  261. }
  262. }
  263. if len(labels) > 0 {
  264. s.Label = labels
  265. }
  266. if len(numLabels) > 0 {
  267. s.NumLabel = numLabels
  268. }
  269. s.Location = make([]*Location, len(s.locationIDX))
  270. for i, lid := range s.locationIDX {
  271. s.Location[i] = locations[lid]
  272. }
  273. s.locationIDX = nil
  274. }
  275. p.DropFrames, err = getString(p.stringTable, &p.dropFramesX, err)
  276. p.KeepFrames, err = getString(p.stringTable, &p.keepFramesX, err)
  277. if pt := p.PeriodType; pt == nil {
  278. p.PeriodType = &ValueType{}
  279. }
  280. if pt := p.PeriodType; pt != nil {
  281. pt.Type, err = getString(p.stringTable, &pt.typeX, err)
  282. pt.Unit, err = getString(p.stringTable, &pt.unitX, err)
  283. }
  284. for _, i := range p.commentX {
  285. var c string
  286. c, err = getString(p.stringTable, &i, err)
  287. p.Comments = append(p.Comments, c)
  288. }
  289. p.commentX = nil
  290. p.DefaultSampleType, err = getString(p.stringTable, &p.defaultSampleTypeX, err)
  291. p.stringTable = nil
  292. return err
  293. }
  294. func (p *ValueType) decoder() []decoder {
  295. return valueTypeDecoder
  296. }
  297. func (p *ValueType) encode(b *buffer) {
  298. encodeInt64Opt(b, 1, p.typeX)
  299. encodeInt64Opt(b, 2, p.unitX)
  300. }
  301. var valueTypeDecoder = []decoder{
  302. nil, // 0
  303. // optional int64 type = 1
  304. func(b *buffer, m message) error { return decodeInt64(b, &m.(*ValueType).typeX) },
  305. // optional int64 unit = 2
  306. func(b *buffer, m message) error { return decodeInt64(b, &m.(*ValueType).unitX) },
  307. }
  308. func (p *Sample) decoder() []decoder {
  309. return sampleDecoder
  310. }
  311. func (p *Sample) encode(b *buffer) {
  312. encodeUint64s(b, 1, p.locationIDX)
  313. encodeInt64s(b, 2, p.Value)
  314. for _, x := range p.labelX {
  315. encodeMessage(b, 3, x)
  316. }
  317. }
  318. var sampleDecoder = []decoder{
  319. nil, // 0
  320. // repeated uint64 location = 1
  321. func(b *buffer, m message) error { return decodeUint64s(b, &m.(*Sample).locationIDX) },
  322. // repeated int64 value = 2
  323. func(b *buffer, m message) error { return decodeInt64s(b, &m.(*Sample).Value) },
  324. // repeated Label label = 3
  325. func(b *buffer, m message) error {
  326. s := m.(*Sample)
  327. n := len(s.labelX)
  328. s.labelX = append(s.labelX, label{})
  329. return decodeMessage(b, &s.labelX[n])
  330. },
  331. }
  332. func (p label) decoder() []decoder {
  333. return labelDecoder
  334. }
  335. func (p label) encode(b *buffer) {
  336. encodeInt64Opt(b, 1, p.keyX)
  337. encodeInt64Opt(b, 2, p.strX)
  338. encodeInt64Opt(b, 3, p.numX)
  339. }
  340. var labelDecoder = []decoder{
  341. nil, // 0
  342. // optional int64 key = 1
  343. func(b *buffer, m message) error { return decodeInt64(b, &m.(*label).keyX) },
  344. // optional int64 str = 2
  345. func(b *buffer, m message) error { return decodeInt64(b, &m.(*label).strX) },
  346. // optional int64 num = 3
  347. func(b *buffer, m message) error { return decodeInt64(b, &m.(*label).numX) },
  348. }
  349. func (p *Mapping) decoder() []decoder {
  350. return mappingDecoder
  351. }
  352. func (p *Mapping) encode(b *buffer) {
  353. encodeUint64Opt(b, 1, p.ID)
  354. encodeUint64Opt(b, 2, p.Start)
  355. encodeUint64Opt(b, 3, p.Limit)
  356. encodeUint64Opt(b, 4, p.Offset)
  357. encodeInt64Opt(b, 5, p.fileX)
  358. encodeInt64Opt(b, 6, p.buildIDX)
  359. encodeBoolOpt(b, 7, p.HasFunctions)
  360. encodeBoolOpt(b, 8, p.HasFilenames)
  361. encodeBoolOpt(b, 9, p.HasLineNumbers)
  362. encodeBoolOpt(b, 10, p.HasInlineFrames)
  363. }
  364. var mappingDecoder = []decoder{
  365. nil, // 0
  366. func(b *buffer, m message) error { return decodeUint64(b, &m.(*Mapping).ID) }, // optional uint64 id = 1
  367. func(b *buffer, m message) error { return decodeUint64(b, &m.(*Mapping).Start) }, // optional uint64 memory_offset = 2
  368. func(b *buffer, m message) error { return decodeUint64(b, &m.(*Mapping).Limit) }, // optional uint64 memory_limit = 3
  369. func(b *buffer, m message) error { return decodeUint64(b, &m.(*Mapping).Offset) }, // optional uint64 file_offset = 4
  370. func(b *buffer, m message) error { return decodeInt64(b, &m.(*Mapping).fileX) }, // optional int64 filename = 5
  371. func(b *buffer, m message) error { return decodeInt64(b, &m.(*Mapping).buildIDX) }, // optional int64 build_id = 6
  372. func(b *buffer, m message) error { return decodeBool(b, &m.(*Mapping).HasFunctions) }, // optional bool has_functions = 7
  373. func(b *buffer, m message) error { return decodeBool(b, &m.(*Mapping).HasFilenames) }, // optional bool has_filenames = 8
  374. func(b *buffer, m message) error { return decodeBool(b, &m.(*Mapping).HasLineNumbers) }, // optional bool has_line_numbers = 9
  375. func(b *buffer, m message) error { return decodeBool(b, &m.(*Mapping).HasInlineFrames) }, // optional bool has_inline_frames = 10
  376. }
  377. func (p *Location) decoder() []decoder {
  378. return locationDecoder
  379. }
  380. func (p *Location) encode(b *buffer) {
  381. encodeUint64Opt(b, 1, p.ID)
  382. encodeUint64Opt(b, 2, p.mappingIDX)
  383. encodeUint64Opt(b, 3, p.Address)
  384. for i := range p.Line {
  385. encodeMessage(b, 4, &p.Line[i])
  386. }
  387. }
  388. var locationDecoder = []decoder{
  389. nil, // 0
  390. func(b *buffer, m message) error { return decodeUint64(b, &m.(*Location).ID) }, // optional uint64 id = 1;
  391. func(b *buffer, m message) error { return decodeUint64(b, &m.(*Location).mappingIDX) }, // optional uint64 mapping_id = 2;
  392. func(b *buffer, m message) error { return decodeUint64(b, &m.(*Location).Address) }, // optional uint64 address = 3;
  393. func(b *buffer, m message) error { // repeated Line line = 4
  394. pp := m.(*Location)
  395. n := len(pp.Line)
  396. pp.Line = append(pp.Line, Line{})
  397. return decodeMessage(b, &pp.Line[n])
  398. },
  399. }
  400. func (p *Line) decoder() []decoder {
  401. return lineDecoder
  402. }
  403. func (p *Line) encode(b *buffer) {
  404. encodeUint64Opt(b, 1, p.functionIDX)
  405. encodeInt64Opt(b, 2, p.Line)
  406. }
  407. var lineDecoder = []decoder{
  408. nil, // 0
  409. // optional uint64 function_id = 1
  410. func(b *buffer, m message) error { return decodeUint64(b, &m.(*Line).functionIDX) },
  411. // optional int64 line = 2
  412. func(b *buffer, m message) error { return decodeInt64(b, &m.(*Line).Line) },
  413. }
  414. func (p *Function) decoder() []decoder {
  415. return functionDecoder
  416. }
  417. func (p *Function) encode(b *buffer) {
  418. encodeUint64Opt(b, 1, p.ID)
  419. encodeInt64Opt(b, 2, p.nameX)
  420. encodeInt64Opt(b, 3, p.systemNameX)
  421. encodeInt64Opt(b, 4, p.filenameX)
  422. encodeInt64Opt(b, 5, p.StartLine)
  423. }
  424. var functionDecoder = []decoder{
  425. nil, // 0
  426. // optional uint64 id = 1
  427. func(b *buffer, m message) error { return decodeUint64(b, &m.(*Function).ID) },
  428. // optional int64 function_name = 2
  429. func(b *buffer, m message) error { return decodeInt64(b, &m.(*Function).nameX) },
  430. // optional int64 function_system_name = 3
  431. func(b *buffer, m message) error { return decodeInt64(b, &m.(*Function).systemNameX) },
  432. // repeated int64 filename = 4
  433. func(b *buffer, m message) error { return decodeInt64(b, &m.(*Function).filenameX) },
  434. // optional int64 start_line = 5
  435. func(b *buffer, m message) error { return decodeInt64(b, &m.(*Function).StartLine) },
  436. }
  437. func addString(strings map[string]int, s string) int64 {
  438. i, ok := strings[s]
  439. if !ok {
  440. i = len(strings)
  441. strings[s] = i
  442. }
  443. return int64(i)
  444. }
  445. func getString(strings []string, strng *int64, err error) (string, error) {
  446. if err != nil {
  447. return "", err
  448. }
  449. s := int(*strng)
  450. if s < 0 || s >= len(strings) {
  451. return "", errMalformed
  452. }
  453. *strng = 0
  454. return strings[s], nil
  455. }