There are several reasons for muscle spasms/cramps, but they basically boil down to one of two causes:
- Some sort of bogus nerve activation
- The inability of the muscles to "reset" themselves
The bogus nerve activation situation can occur because of some sort of nerve irritation, or due to one of several somewhat rare and rather serious medical conditions. Given that the latter are unlikely, nerve irritation -- either in the spine or where the nerves pass through the piriformis muscle in the butt -- would be the possibility to be concerned with. But if you had this you'd likely be complaining of "electric" sensations running down the leg.
So most likely the problem is with the muscles being unable to "reset".
It's important to understand how muscle cells work. They do not need (new) energy to contract. Rather, a muscle cell is like a stretched spring that's been propped into the stretched position -- it's ready to contract as soon as the "prop" is removed. To remove the "prop" and allow the muscle to contract, the associated neuron causes calcium to be released into the muscle cells. This triggers a cascade of chemical reactions and the muscle contracts.
To "reset" the muscle the calcium must be "pumped" out of the cells and energy (glucose converted to ATP) must be used to mechanically re-extend the muscle fiber.
A cramp occurs when something prevents this "reset".
The two likely causes of this inability to reset are a failure of the calcium "pump" and the simple absence of sufficient energy to do the reset. This can be more than a simply unpleasant situation, since if a muscle cell is fully "depolarized" for long enough (more than a minute or so) it dies. (Thankfully, this full depolarization occurs fairly rarely in healthy individuals, though it's not uncommon in football "boot camps" when run by clueless coaches.)
Several factors are involved in the calcium pump mechanism, but, in addition to glucose/ATP, the "electrolytes" sodium, potassium, and magnesium play important roles.
(There are also several genetic disorders such as myoadenylate deaminase deficiency and McArdle's Disease that can cause "reset" cramps in susceptible individuals. And MADD is actually fairly common, affecting about one individual in 50.)
So basically a shortage of glucose, sodium, or potassium can lead to failure to reset and associated cramping. A shortage of glucose will generally affect a small portion of the muscle being most strenuously exercised, vs causing a cramp of an entire muscle group. So if your cramp is widespread and not isolated to a small portion of muscle then it's probably due to electrolyte imbalance.
In particular, when major muscles are being exercised to the max and the body is running low on electrolytes, it will "borrow" them from muscles that are not being so vigorously exercised. So you may experience, eg, cramps of the muscles along the inside of the legs, even though you're not exercising those muscles to any significant degree. (But you might also experience cramps of the muscles you ARE exercising vigorously, depending on a number of factors.)
A widespread cramp is thus generally a sign of a electrolyte imbalance, and ingesting more sodium, potassium, and/or magnesium is probably called for. (The body has substantial stores of calcium in the bones, so a short-term deficiency of calcium is unlikely. Plus, a localized shortage of calcium causes a sort of paralysis vs cramping.)
Note that what you experience a day or two after the cramp gives some clue as to what happened. With a simple cramp the affected muscles are apt to be sore the next day (and for maybe a day or two thereafter), just as if you had overworked them in the gym. But if the pain takes 36-48 hours to appear (and the pain remains for weeks) it's likely some degree of rhabdomyolysis -- actual muscle injury due to depolarized muscle cells. This would be a clue that you're doing something seriously wrong.