t23247.nim 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. discard """
  2. matrix: ";-d:useMalloc"
  3. """
  4. # bug #23247
  5. import std/hashes
  6. func baseAddr[T](x: openArray[T]): ptr T =
  7. # Return the address of the zero:th element of x or `nil` if x is empty
  8. if x.len == 0: nil else: cast[ptr T](x)
  9. func makeUncheckedArray[T](p: ptr T): ptr UncheckedArray[T] =
  10. cast[ptr UncheckedArray[T]](p)
  11. type
  12. LabelKey = object
  13. data: seq[string]
  14. refs: ptr UncheckedArray[string]
  15. refslen: int
  16. Gauge = ref object
  17. metrics: seq[seq[seq[string]]]
  18. template values(key: LabelKey): openArray[string] =
  19. if key.refslen > 0:
  20. key.refs.toOpenArray(0, key.refslen - 1)
  21. else:
  22. key.data
  23. proc hash(key: LabelKey): Hash =
  24. hash(key.values)
  25. proc view(T: type LabelKey, values: openArray[string]): T =
  26. # TODO some day, we might get view types - until then..
  27. LabelKey(refs: baseAddr(values).makeUncheckedArray(), refslen: values.len())
  28. template withValue2(k: untyped) =
  29. discard hash(k)
  30. proc setGauge(
  31. collector: Gauge,
  32. labelValues: openArray[string],
  33. ) =
  34. let v = LabelKey.view(labelValues)
  35. withValue2(v)
  36. collector.metrics.add @[@labelValues, @labelValues]
  37. discard @labelValues
  38. var nim_gc_mem_bytes = Gauge()
  39. let threadID = $getThreadId()
  40. setGauge(nim_gc_mem_bytes, @[threadID])
  41. setGauge(nim_gc_mem_bytes, @[threadID])