Hey, thought I'd just ass my 0.2c so you know you're not alone. I get the same feeling you describe; I'll usually wake up in the middle of the night and have one particular thought, phrase or small series of actions in my head, I'll then spend the next however long (time seems to be pretty meaningless at that point) tossing and turning, not quite asleep but not awake either. Finally I become more awake and begin to try and break away from the cycling thought, it just feels like its repeating it's actions so fast it's hard to interrupt with logical thought. These days, I'll get up ad drink a glass of cold water of I have the energy, or lay in bed and try to focus on my breathing while not allowing my thoughts to wonder...the getting out of bed one is more successful I guess.
I feel that I used to suffer with what some people have mentioned when I was a child, the feeling of the void, the explosion of silence, that sensation where you feel like you lose perspective of size and distance in you're environment. This is fairly common from what I've read, heard and it is not the same thing as what OP is explaining. In honesty, it's pleasant compared to the millions of thoughs per second issue, which makes you think you might genuinely have gone insane (and maybe, temporarily do).
So, this only started happening after I was 20+. In the few years prior to the first bout I'd suffered a severe head injury in a car crash, I had stopped smoking weed after doing so intensively for several years (due to paranoia) and I was a weekend user of MDMA, I also suffer from mild anxiety issues in general life. These are some potential causes for me, any similarities with you?