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

High CPU usage on PyCharm #475

Open
jcfernandez-890825 opened this issue Jun 24, 2024 · 14 comments
Open

High CPU usage on PyCharm #475

jcfernandez-890825 opened this issue Jun 24, 2024 · 14 comments

Comments

@jcfernandez-890825
Copy link

jcfernandez-890825 commented Jun 24, 2024

While I was trying to create a script to do auto-upgrades, my laptop CPU usage went of the chart all of a sudden, and now is very hard to add just 1 line of code.

I have a 32GB of RAM and a 12th Gen Intel Core i7-1270P with 12 cores, so I don't think this is a hardware issue.

Here is the code

# -*- coding: utf-8 -*-
# set server timezone in UTC before time module imported
__import__('os').environ['TZ'] = 'UTC'

import logging
import sys

import odoo
from odoo.tools import config


# Get the logger
_logger = logging.getLogger(__name__)


def _upd_db(cr, db_nm):
    # Get the superuser ID
    uid = odoo.SUPERUSER_ID
    # Get the environment context
    ctx = odoo.api.Environment(cr, uid, {})['res.users'].context_get()
    # Get the environment
    env = odoo.api.Environment(cr, uid, ctx)
    # Get the module model
    Mdl = env['ir.module.module']
    # Update module list
    Mdl.update_list()
    # Find module `xgo_mdl_auto_upd`
    xgo_mdl_auto_upd = Mdl.search([('name', '=', 'xgo_mdl_auto_upd')])
    # If module `xgo_mdl_auto_upd` does not exist:
    if not xgo_mdl_auto_upd:
        # Log a message and continue
        _logger.error('Module `xgo_mdl_auto_upd` does not exist')
        return
    # If module `xgo_mdl_auto_upd` is uninstalled
    if xgo_mdl_auto_upd.state == 'uninstalled':
        # Install module `xgo_mdl_auto_upd`
        xgo_mdl_auto_upd.button_immediate_install()
        # Since the module `xgo_mdl_auto_upd` was installed.
        # We need to remove the saved checksums to force the update of all modules.
        from odoo.addons.module_auto_update.models.module import PARAM_INSTALLED_CHECKSUMS
        env["ir.config_parameter"].set_param(PARAM_INSTALLED_CHECKSUMS, "{}")
        # Commit the changes
        env.cr.commit()
        # Return False to indicate that the database was not processed
        return False
    # If module `xgo_mdl_auto_upd` installation was unfinished
    elif xgo_mdl_auto_upd.state == 'to install':
        # Cancel the installation
        xgo_mdl_auto_upd.button_install_cancel()
        # Commit the changes
        env.cr.commit()
        # Return False to indicate that the database was not processed
        return False
    # If module `xgo_mdl_auto_upd` is not installed
    elif xgo_mdl_auto_upd.state != 'installed':
        # Get module `xgo_mdl_auto_upd` state
        xgo_mdl_auto_upd_state = xgo_mdl_auto_upd.state
        # Get the module states as a dictiona
        mdl_sts_dct = dict(Mdl._fields['state']._description_selection(Mdl.env))
        # Log a message and continue
        _logger.error(
            'Module `xgo_mdl_auto_upd` state is "%s" instead of "%s"',
            mdl_sts_dct[xgo_mdl_auto_upd_state], mdl_sts_dct['installed']
        )
        return
    # Update all modules with changes checksums
    getattr(Mdl, 'upgrade_changed_checksum')()
    # Commit the changes
    env.cr.commit()
    # Log a message
    _logger.info('Database "%s" processed successfully', db_nm)


def main():
    # Get the logger
    global _logger
    # Get the arguments
    args = sys.argv[1:]
    # Set the program name in the configuration parser
    config.parser.prog = 'xgo_cli_auto_upd'
    # Parse the configuration
    config.parse_config(args)
    # Report the configuration of the server
    odoo.cli.server.report_configuration()
    # Get the database list
    db_lst = odoo.service.db.list_dbs(True)
    # Get a list of incompatible databases
    bad_db_lst = odoo.service.db.list_db_incompatible(db_lst)
    # Filter-out the incompatible databases
    db_lst = list(set(db_lst) - set(bad_db_lst))
    # Sort the database list
    db_lst.sort()
    # For each database in the list
    for db_nm in db_lst:
        # Add 50 lines to the log
        _logger.info('\n' * 50)
        # Log a message
        _logger.info('Processing database "%s"', db_nm)
        # Unfinished variable
        unfinished = True
        while unfinished:
            # Get model registry
            registry = odoo.registry(db_nm)
            # Get the cursor
            with registry.cursor() as cr:
                try:
                    # Perform the database update
                    # If the database was not processed (False is returned)
                    unfinished = _upd_db(cr, db_nm) is False
                except Exception as e:
                    # Log the exception
                    _logger.exception('Failed to update database:\n%s', e)
                    # Rollback the transaction
                    cr.rollback()
                    # Break the loop
                    break


if __name__ == "__main__":
    main()

Could you please add this code into a file and try editing it?

@trinhanhngoc
Copy link
Member

Hi @jcfernandez-890825 ,

I just tested your code on Macbook Pro M1 16GB. Everything is working fine without any performance issues.

@jcfernandez-890825
Copy link
Author

Hi @trinhanhngoc

Mmmm... this is weird ...

As soon as I disable the plugin the CPU usage goes away, maybe there is a problem when the Odoo plugin interacts with another plugin ior feature?
Is there a inspection/analysis setting thing that might be causing this?

@trinhanhngoc
Copy link
Member

@jcfernandez-890825 ,

I need more information: PyCharm version? Odoo version? Other plugins?

@jcfernandez-890825
Copy link
Author

@trinhanhngoc

Here is my info:

PyCharm 2024.1.4 (Professional Edition)
Build #PY-241.18034.82, built on June 24, 2024
Licensed to Juan Carlos Fernández
Subscription is active until November 13, 2024.
Runtime version: 17.0.11+1-b1207.24 amd64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
Linux 5.15.0-107-generic
GC: G1 Young Generation, G1 Old Generation
Memory: 10240M
Cores: 16
Non-Bundled Plugins:
  com.jetbrains.space (241.18034.4)
  Statistic (4.2.14)
  dev.meanmail.plugin.nginx-intellij-plugin (2024.3)
  XPathView (241.15989.65)
  net.seesharpsoft.intellij.plugins.csv (3.3.0-241)
  com.github.copilot (1.5.11.5872)
  dev.ngocta.pycharm-odoo (2024.5.1.241)
  com.intellij.bigdatatools.core (241.18034.62)
  com.intellij.bigdatatools.binary.files (241.14494.158)
  com.intellij.bigdatatools.rfs (241.18034.62)
  zielu.gittoolbox (500.2.10+233)
Current Desktop: X-Cinnamon

@jcfernandez-890825
Copy link
Author

@trinhanhngoc

Since this is not a module, but just a python package with DevOps scripts.
image

Maybe is indexing constantly all modules because there is no manifest?

@jcfernandez-890825
Copy link
Author

@trinhanhngoc

I just tested your code on Macbook Pro M1 16GB. Everything is working fine without any performance issues.

When you did this did you added the code inside a module folder?
When I add a manifest file to xgo_dev_ops. The problem stops.

@trinhanhngoc
Copy link
Member

@jcfernandez-890825 ,

No, I created a temp file in the root of the Odoo source code.

image

@trinhanhngoc trinhanhngoc changed the title High CPU usage on Pycharm when High CPU usage on PyCharm Jul 4, 2024
@imlopes
Copy link

imlopes commented Aug 14, 2024

I have the same problem on my pyCharm :

%CPU Subsystem
...
162.1 Plugin Odoo: dev.ngocta.pycharm.odoo.module
...

PyCharm 2024.1.6 (Professional Edition)
Build #PY-241.19072.16, built on August 8, 2024

Runtime version: 17.0.11+1-b1207.30 aarch64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
macOS 14.5
GC: G1 Young Generation, G1 Old Generation
Memory: 4096M
Cores: 10
Metal Rendering is ON

dev.ngocta.pycharm-odoo (2024.6.1.241)

@trinhanhngoc
Copy link
Member

@imlopes ,

How long does the plugin make high CPU usage when you check?

@imlopes
Copy link

imlopes commented Aug 16, 2024

@imlopes ,

How long does the plugin make high CPU usage when you check?

Now it's deactivated on my machine but the problem is :

  • When I'm writing a code line (so I think the plugin try to load some things)
  • When I'm using the Run Configuration (I had to change to the normal Python Run Configuration because the plugin was freezing my pyCharm)

For information :

  • I'm using docker compose
  • I work on projects with many OCA submodules

Here my run configuration when I was using the plugin :

<component name="ProjectRunConfigurationManager"> <configuration default="false" name="Odoo" type="Odoo" factoryName="Odoo" nameIsGenerated="true"> <module name="PROJECT" /> <option name="ENV_FILES" value="" /> <option name="INTERPRETER_OPTIONS" value="" /> <option name="PARENT_ENVS" value="true" /> <envs> <env name="PYTHONUNBUFFERED" value="1" /> </envs> <option name="SDK_HOME" value="" /> <option name="WORKING_DIRECTORY" value="/odoo" /> <option name="IS_MODULE_SDK" value="true" /> <option name="ADD_CONTENT_ROOTS" value="true" /> <option name="ADD_SOURCE_ROOTS" value="true" /> <EXTENSION ID="PythonCoverageRunConfigurationExtension" runner="coverage.py" /> <PathMappingSettings> <option name="pathMappings"> <list> <mapping local-root="$PROJECT_DIR$/PROJECT_odoo/odoo" remote-root="/odoo" /> </list> </option> </PathMappingSettings> <option name="SCRIPT_NAME" value="/odoo/src/odoo-bin" /> <option name="PARAMETERS" value="--dev xml" /> <option name="SHOW_COMMAND_LINE" value="false" /> <option name="EMULATE_TERMINAL" value="true" /> <method v="2" /> </configuration> </component>%

And reading this comment I think I don't give you the answer for your question, so :

  • How long does the plugin make high CPU usage when you check?
  • It depends. Some times was fast, I mean, less than 1 sec (the time to display the high usage and then drop down to less than 40%) but when using Run Conf, it was long long time

@trinhanhngoc
Copy link
Member

trinhanhngoc commented Aug 16, 2024

@imlopes ,

Thanks for the additional information. Can you capture screenshots of the Activity Monitor window when the plugin makes high CPU usage for a long time (with Odoo Run Configuration)?

image

@imlopes
Copy link

imlopes commented Aug 23, 2024

@trinhanhngoc

https://drive.google.com/file/d/10Ciotfj-OFzFMTvK7-Yo1zKVVS85xME7/view?usp=sharing

I can't use the plugin :/
Maybe I have a problem with the indexes on my project as I use a lot of submodules.
In this video, I was not using the Run Conf, I was just typing my code and IMHO the autocomp is the problem :/

@trinhanhngoc
Copy link
Member

@imlopes ,

The high load is strange. Is the source code you are using public on github?. I want to reproduce the problem on my laptop.

@imlopes
Copy link

imlopes commented Aug 24, 2024

Yes, I agreed w you, is really strange.
My project is private but maybe you can "reproduce" creating an odoo project and then, adding many submodules:

[submodule "odoo/src"]
	path = odoo/src
	url = [email protected]:OCA/odoo.git
	branch = 16.0

[submodule "odoo/external-src/crm"]
	path = odoo/external-src/crm
	url = [email protected]:OCA/crm.git
	branch = 16.0

[submodule "odoo/external-src/hr"]
	path = odoo/external-src/hr
	url = [email protected]:OCA/hr.git
	branch = 16.0

[submodule "odoo/external-src/partner-contact"]
	path = odoo/external-src/partner-contact
	url = [email protected]:OCA/partner-contact.git
	branch = 16.0

[submodule "odoo/external-src/report-print-send"]
	path = odoo/external-src/report-print-send
	url = [email protected]:OCA/report-print-send.git
	branch = 16.0
[submodule "odoo/external-src/reporting-engine"]
	path = odoo/external-src/reporting-engine
	url = [email protected]:OCA/reporting-engine.git
	branch = 16.0

[submodule "odoo/external-src/server-auth"]
	path = odoo/external-src/server-auth
	url = [email protected]:OCA/server-auth.git
	branch = 16.0
[submodule "odoo/external-src/server-backend"]
	path = odoo/external-src/server-backend
	url = [email protected]:OCA/server-backend.git
	branch = 16.0
[submodule "odoo/external-src/server-brand"]
	path = odoo/external-src/server-brand
	url = [email protected]:OCA/server-brand.git
	branch = 16.0
[submodule "odoo/external-src/server-env"]
	path = odoo/external-src/server-env
	url = [email protected]:OCA/server-env.git
	branch = 16.0
[submodule "odoo/external-src/server-tools"]
	path = odoo/external-src/server-tools
	url = [email protected]:OCA/server-tools.git
	branch = 16.0
[submodule "odoo/external-src/server-ux"]
	path = odoo/external-src/server-ux
	url = [email protected]:OCA/server-ux.git
	branch = 16.0

And so on...
And use this structure : https://github.com/camptocamp/docker-odoo-project/tree/master/example

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

3 participants