Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CPU usage and pause problem #5

Open
chihsheng1220 opened this issue Mar 21, 2018 · 6 comments
Open

CPU usage and pause problem #5

chihsheng1220 opened this issue Mar 21, 2018 · 6 comments

Comments

@chihsheng1220
Copy link

chihsheng1220 commented Mar 21, 2018

Hi,

I tried to use your project example(randomPMU.py) running on Raspberry pi 3 couple weeks ago, and I found that cpu usage was up to 100%. Sometimes, it paused during the process of sending data message.
201803013 3
After I checked task mamager, it was created a lots of PID.
201803013 4
Need a help to solve this problem.

Thanks,

Jim

@sstevan
Copy link
Contributor

sstevan commented Mar 21, 2018

Hello @chihsheng1220,

How many PDCs are you trying to connect with? PMU will spawn a new process (PID) for each connection.

If you try to decrease your reporting rage it might help as well.
You should try single connection with 5 frames/second and gradually
increase until you reach the limits.

@chihsheng1220
Copy link
Author

chihsheng1220 commented Mar 27, 2018

Hi @sstevan ,

Only one PDC I was trying to connect with. I have tried with 1 frames/second already; however, the problem still existed. I have no idea which part is improper. Do you have any suggestion to deal with it?

Code:

import random
import Adafruit_DHT #DHT22 library
import RPi.GPIO as GPIO #GPIO pin library
import time
from synchrophasor.frame import ConfigFrame2
from synchrophasor.pmu import Pmu

if __name__ == "__main__":

    GPIO.setmode(GPIO.BOARD) #Set GPIO mode
    GPIO.setup(11, GPIO.IN) #Set Pin 11 as an Input Pin 

    pmu = Pmu(ip="192.168.1.6", port=4712) #Device ip & port
    pmu.logger.setLevel("DEBUG")

    cfg = ConfigFrame2(1,  # PMU_ID
                       1000000,  # TIME_BASE
                       1,  # Number of PMUs included in data frame
                       "Edit Station",  # Station name
                       33,  # Data-stream ID(s)
                       (True, True, True, True),  # Data format - POLAR; PH - REAL; AN - REAL; FREQ - REAL;
                       3,  # Number of phasors
                       4,  # Number of analog values
                       4,  # Number of digital status words
                       ["VA", "VB", "VC", "ANALOG1", "ANALOG2", "ANALOG3", "ANALOG4", "BREAKER 01 STATUS",
                        "BREAKER 02 STATUS", "BREAKER 03 STATUS", "BREAKER 04 STATUS", "BREAKER 05 STATUS",
                        "BREAKER 06 STATUS", "BREAKER 07 STATUS", "BREAKER 08 STATUS", "BREAKER 09 STATUS",
                        "BREAKER 0A STATUS", "BREAKER 0B STATUS", "BREAKER 0C STATUS", "BREAKER 0D STATUS",
                        "BREAKER 0E STATUS", "BREAKER 0F STATUS", "BREAKER 0G STATUS", "BREAKER 11 STATUS",
                        "BREAKER 12 STATUS", "BREAKER 13 STATUS", "BREAKER 14 STATUS", "BREAKER 15 STATUS",
                        "BREAKER 16 STATUS", "BREAKER 17 STATUS", "BREAKER 18 STATUS", "BREAKER 19 STATUS",
                        "BREAKER 1A STATUS", "BREAKER 1B STATUS", "BREAKER 1C STATUS", "BREAKER 1D STATUS",
                        "BREAKER 1E STATUS", "BREAKER 1F STATUS", "BREAKER 1G STATUS", "BREAKER 21 STATUS",
                        "BREAKER 22 STATUS", "BREAKER 23 STATUS", "BREAKER 24 STATUS", "BREAKER 25 STATUS",
                        "BREAKER 26 STATUS", "BREAKER 27 STATUS", "BREAKER 28 STATUS", "BREAKER 29 STATUS",
                        "BREAKER 2A STATUS", "BREAKER 2B STATUS", "BREAKER 2C STATUS", "BREAKER 2D STATUS",
                        "BREAKER 2E STATUS", "BREAKER 2F STATUS", "BREAKER 2G STATUS", "BREAKER 31 STATUS",
                        "BREAKER 32 STATUS", "BREAKER 33 STATUS", "BREAKER 34 STATUS", "BREAKER 35 STATUS",
                        "BREAKER 36 STATUS", "BREAKER 37 STATUS", "BREAKER 38 STATUS", "BREAKER 39 STATUS",
                        "BREAKER 3A STATUS", "BREAKER 3B STATUS", "BREAKER 3C STATUS", "BREAKER 3D STATUS",
                        "BREAKER 3E STATUS", "BREAKER 3F STATUS", "BREAKER 3G STATUS"],  # Channel Names
                       [(0, "v"), (0, "v"),
                        (0, "v")],  # Conversion factor for phasor channels - (float representation, not important)
                       [(1, "pow"), (1, "pow"), (1, "pow"), (1, "pow")],  # Conversion factor for analog channels
                       [(0x0000, 0xffff), (0x0000, 0xffff), (0x0000, 0xffff), (0x0000, 0xffff)],  # Mask words for digital status words
                       50,  # Nominal frequency
                       1,  # Configuration change count
                       1)  # Rate of phasor data transmission)

    pmu.set_configuration(cfg)
    pmu.set_header("This is EDIT PMU")

    pmu.run()

    while True:
        if pmu.clients:
            DI = GPIO.input(11) #Set Input Pin 11 as a Digital In
            humidity, temperature = Adafruit_DHT.read_retry(22, 4) #Keep reading DHT22 Pin 4
            temp = 'Temp: {0:0.1f} C'.format(temperature)
            humi = 'Humidity: {0:0.1f} %'.format(humidity)
            pmu.send_data(phasors=[(random.uniform(215.0, 240.0), random.uniform(-0.1, 0.3)),
                                  (random.uniform(215.0, 240.0), random.uniform(1.9, 2.2)),
                                  (random.uniform(215.0, 240.0), random.uniform(3.0, 3.14))],
                          analog=[temperature, humidity, random.randint(0, 100), random.uniform(0, 100)],
                          digital=[1234, DI, 0x0001, random.randint(0, 1)])
                 
    pmu.join()

Thanks for your help.

Jim

@sstevan
Copy link
Contributor

sstevan commented May 29, 2018

Hey @chihsheng1220, sorry for the huge delay.

Can you please verify that solution offered by @novicas really solves your issue (check pull request #6 )?

You can actually change only this part of the script from above and let us know if it works:

    data_rate = 30  # E.g. 30 measurements per second
    delay = (1.0 / data_rate) if data_rate > 0 else -data_rate

    while True:
        if pmu.clients:
            time.sleep(delay)   #  ----->    Do not flood queue    <----------
            DI = GPIO.input(11) #Set Input Pin 11 as a Digital In
            humidity, temperature = Adafruit_DHT.read_retry(22, 4) #Keep reading DHT22 Pin 4
            temp = 'Temp: {0:0.1f} C'.format(temperature)
            humi = 'Humidity: {0:0.1f} %'.format(humidity)
            pmu.send_data(phasors=[(random.uniform(215.0, 240.0), random.uniform(-0.1, 0.3)),
                                  (random.uniform(215.0, 240.0), random.uniform(1.9, 2.2)),
                                  (random.uniform(215.0, 240.0), random.uniform(3.0, 3.14))],
                          analog=[temperature, humidity, random.randint(0, 100), random.uniform(0, 100)],
                          digital=[1234, DI, 0x0001, random.randint(0, 1)])
        else:
            time.sleep(1)  # -------> Just wait for some time to get a new connection   <----------

@chihsheng1220
Copy link
Author

Hi @sstevan ,

I have tried added some execution pause already, but the problem still existed. I used PMU Connection Tester to simulate reconnecting situation. PMU will spawn a new process (PID) for each connection and never be killed. How could I close each disconnection process? By the way, PMU crashed if I use "kill -9 PID" command for killing PID.
pid2
pid
Thanks for your help.

Jim

@sstevan
Copy link
Contributor

sstevan commented Jun 15, 2018

Hey @chihsheng1220,

Have you tried the script above?
Please share your script with a delay so we can test it as well.

@chihsheng1220
Copy link
Author

Yes, I've tried the script above already.
`
#Date: 2018/06/15
#Editor: Jim Chen
#Version: 1.0.0

import lcd_i2c_driver
import random
import Adafruit_GPIO.SPI as SPI
import Adafruit_MCP3008
import RPi.GPIO as GPIO #GPIO pin library
import os
from time import *
import time
from datetime import datetime
from synchrophasor.frame import ConfigFrame2
from synchrophasor.pmu import Pmu

time.sleep(10)

SPI_PORT = 0
SPI_DEVICE = 0
mcp = Adafruit_MCP3008.MCP3008(spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE))
mylcd = lcd_i2c_driver.lcd()

if name == "main":

GPIO.setmode(GPIO.BOARD) #Set GPIO mode    
GPIO.setup(11, GPIO.IN) #Set Pin 11 as an Input Pin
GPIO.setup(13, GPIO.IN) #set Pin 13
GPIO.setup(15, GPIO.OUT)

pwm_led = GPIO.PWM(15, 500)
pwm_led.start(0)

pmu = Pmu(ip="10.11.1.6", port=4712) #Device 
#pmu.logger.setLevel("DEBUG")

data_rate = 60
cfg = ConfigFrame2(1,  # PMU_ID
                   1000000,  # TIME_BASE
                   1,  # Number of PMUs included in data frame
                   "Edit Station",  # Station name
                   33,  # Data-stream ID(s)
                   (True, True, True, True),  # Data format - POLAR; PH - REAL; AN - REAL; FREQ - REAL;
                   4,  # Number of phasors
                   5,  # Number of analog values
                   4,  # Number of digital status words
                   ["VA", "VB", "VC", "VD", "ANALOG1", "ANALOG2", "ANALOG3", "ANALOG4", "ANALOG5", "BREAKER 01 STATUS",
                    "BREAKER 02 STATUS", "BREAKER 03 STATUS", "BREAKER 04 STATUS", "BREAKER 05 STATUS",
                    "BREAKER 06 STATUS", "BREAKER 07 STATUS", "BREAKER 08 STATUS", "BREAKER 09 STATUS",
                    "BREAKER 0A STATUS", "BREAKER 0B STATUS", "BREAKER 0C STATUS", "BREAKER 0D STATUS",
                    "BREAKER 0E STATUS", "BREAKER 0F STATUS", "BREAKER 0G STATUS", "BREAKER 11 STATUS",
                    "BREAKER 12 STATUS", "BREAKER 13 STATUS", "BREAKER 14 STATUS", "BREAKER 15 STATUS",
                    "BREAKER 16 STATUS", "BREAKER 17 STATUS", "BREAKER 18 STATUS", "BREAKER 19 STATUS",
                    "BREAKER 1A STATUS", "BREAKER 1B STATUS", "BREAKER 1C STATUS", "BREAKER 1D STATUS",
                    "BREAKER 1E STATUS", "BREAKER 1F STATUS", "BREAKER 1G STATUS", "BREAKER 21 STATUS",
                    "BREAKER 22 STATUS", "BREAKER 23 STATUS", "BREAKER 24 STATUS", "BREAKER 25 STATUS",
                    "BREAKER 26 STATUS", "BREAKER 27 STATUS", "BREAKER 28 STATUS", "BREAKER 29 STATUS",
                    "BREAKER 2A STATUS", "BREAKER 2B STATUS", "BREAKER 2C STATUS", "BREAKER 2D STATUS",
                    "BREAKER 2E STATUS", "BREAKER 2F STATUS", "BREAKER 2G STATUS", "BREAKER 31 STATUS",
                    "BREAKER 32 STATUS", "BREAKER 33 STATUS", "BREAKER 34 STATUS", "BREAKER 35 STATUS",
                    "BREAKER 36 STATUS", "BREAKER 37 STATUS", "BREAKER 38 STATUS", "BREAKER 39 STATUS",
                    "BREAKER 3A STATUS", "BREAKER 3B STATUS", "BREAKER 3C STATUS", "BREAKER 3D STATUS",
                    "BREAKER 3E STATUS", "BREAKER 3F STATUS", "BREAKER 3G STATUS"],  # Channel Names
                   [(0, "v"), (0, "v"),
                    (0, "v"), (0, "v")],  # Conversion factor for phasor channels - (float representation, not important)
                   [(1, "pow"), (1, "pow"), (1, "pow"), (1, "pow"), (1, "pow")],  # Conversion factor for analog channels
                   [(0x0000, 0xffff), (0x0000, 0xffff), (0x0000, 0xffff), (0x0000, 0xffff)],  # Mask words for digital status words
                   50,  # Nominal frequency
                   1,  # Configuration change count
                   data_rate)  # Rate of phasor data transmission)

delay = (1.0 / data_rate)
if data_rate > 0:
 data_rate
else:
 -data_rate

pmu.set_configuration(cfg)
pmu.set_header("This is EDIT PMU")
pmu.run()
counter = 0
while True:
        if pmu.clients:
           time.sleep(delay)
           value_1 = mcp.read_adc(0)
           value_2 = mcp.read_adc(1)
           value_3 = mcp.read_adc(2)
           value_4 = mcp.read_adc(3)               
           voltage_1 = value_1 * 3.3 / 1024
           voltage_2 = value_2 * 3.3 / 1024
           voltage_3 = value_3 * 3.3 / 1024
           voltage_4 = value_4 * 3.3 / 1024               
           DI = GPIO.input(11) #Set Input Pin 11 as a Digital In
           AI = GPIO.input(13) #Set Input Pin 13
           hour = time.localtime().tm_hour
           minute = time.localtime().tm_min
           seconds = time.localtime().tm_sec
           system_time = hour * 10000 + minute *100 + seconds
           file = open("/sys/class/thermal/thermal_zone0/temp")
           system_temp = float(file.read()) /1000
           file.close()
           
           pmu.send_data(phasors=[(value_1, 0),
                        (value_2, 0),
                        (DI, 0), (DI, 0)],
                      analog=[voltage_1, voltage_2, system_time, system_temp, AI],
                      digital=[DI, DI, DI, DI])
                          
           pwm_led.ChangeDutyCycle(counter)
           
           #mylcd.lcd_display_string(strftime("%a, %d %b", localtime()), 1)
           #mylcd.lcd_display_string(strftime("%H:%M:%S", localtime()), 2)
          
           counter += 1
           
           if counter == 100:
            mylcd.lcd_clear()                      
            #mylcd.lcd_display_string(strftime("%a,%d %b %H:%M:%S", localtime()), 1)
            mylcd.lcd_display_string_pos("System Temp:", 1, 0)
            mylcd.lcd_display_string_pos(str(system_temp), 1, 12)
            mylcd.lcd_display_string_pos("V1=", 2, 0)
            mylcd.lcd_display_string_pos(str(value_1), 2, 3)
            mylcd.lcd_display_string_pos("V2=", 2, 9)
            mylcd.lcd_display_string_pos(str(value_2), 2, 12)                  
            counter = 0
           
           file = open("/home/pi/pmu_data_log.csv", "a")
           i=0
           if os.stat("/home/pi/pmu_data_log.csv").st_size == 0:
            file.write("Time,value_1,value_2,voltage_1,voltage_2,system_temp\n")
           i=i+1
           now = datetime.now()
           file.write(str(now)+","+str(value_1)+","+str(value_2)+","+str(voltage_1)+","+str(voltage_2)+","+str(system_temp)+"\n")
           file.flush()
           file.close()
           
        else:
           time.sleep(1) 

#pmu.join()

`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants