The ECU(or at least the better ones) can indeed actively 'search' for the point of pinging as you said. The pinging sensor listens to the combustion, based on that the ECU determines how close it is to knocking. Real, actual knocking is audible even to the untrained human ear, but knock sensors can also detect near knocking situations. And that is the most useful functionality.
In case this gives better performance, and that is a clear requirement, they can advance the timing to the point of near pinging. Obviously not when the power isn't needed, since more spark advance increases NOx emissions due to increased peak pressures and heat. A PID loop continually adjusts the timing to make sure the engine doesn't actually ping, but advances as much as is possible if needed.
Thus, the target value of the PID changes according to the demand of the driver, limited by emission regulations.
So effectively, it continually measures the effect of the fuel on pinging. Not the octane rating of the fuel itself. That's not what you ultimately want to know, but pinging is. I doubt measuring octane rating is even possible with a sensor.