I am trying to use RAPL for capping the power consumption but couldn't find any sample codes to do that. Codes from google/this forum are just using RAPL for measuring power/energy. Could anyone give me an example/demo code how to use RAPL for capping the power consumption of CPU?
Power capping on intel processors is all about writing correct values into a Model Specific Resistor (MSR).
A very nice article on how to read and write MSRs can be found at http://www.mindfruit.co.uk/2012/11/a-linux-kernel-module-for.html
Next, the MSR addresses and the associated explanation can be found in section 14.9 (Chapter 14) of Software Developer Manual (vol 3b part 2) of Haswell. It also has the explanation for how to convert Watt value to bits and also the time interval in bits.
The other easy way is to install the intel_rapl kernel module available at https://github.com/torvalds/linux/blob/master/drivers/powercap/intel_rapl.c. With this you can set power cap in watts for different domains.
Hope this helps.