sysctl kern.sched.topology_spec can be over 16k characters. This code written by @freqlabs to help me as I was troubleshooting other issues on AMD epyc dynamically allocates a buffer instead of a fixed length to fix this issue.
Test on xeon 6242R should work fully
Test on AMD EPYC 7551 should fix sysctl size issue but still have other issues to be resolved.
I think this should be if (sysctlbyname() != 0 && errno != ENOMEM)
No need for the extra byte if you're not zeroing it for paranoia's sake.
Also, it's not hurting anything, but may as well free(spec) down at the end of the function.
Otherwise this looks like a good change.
This build has been tested on dual intel 6242R where turbostat works as expected and and on dual AMD EPYC 7551 32-Core where this code fixes the sysctl issues but fails on futher issues which I have a bug report for here https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=251113