This shows you the differences between two versions of the page.
ep:labs:09 [2021/12/04 01:32] andrei.mirciu |
ep:labs:09 [2025/05/15 21:17] (current) andrei.mirciu [Resources] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Lab 09 - Memory, CPU and Network Monitoring (Windows) ====== | + | ====== Lab 09 - Machine Learning ====== |
===== Objectives ===== | ===== Objectives ===== | ||
- | * **Find the root cause if the system runs out of memory** with Task Manager, Windows Performance Recorder, Windows Performance Analyzer, Visual Studio, VMMap. | + | * Understand basic concepts of machine learning |
- | * **Monitor the CPU usage** with Task Manager, Windows Performance Recorder, Windows Performance Analyzer. | + | * Remember examples of real-world problems that can be solved with machine learning |
- | * **Check the amount of network traffic generated by processes** with Task Manager, Windows Performance Recorder, Microsoft Network Monitoring, Wireshark. | + | * Learn the most common performance evaluation metrics for machine learning models |
+ | * Analyse the behaviour of typical machine learning algorithms using the most popular techniques | ||
+ | * Be able to compare multiple machine learning models | ||
- | <note important> | + | ===== Resources ===== |
- | You can download the **Windows 10 VM** via [[https://ctipub-my.sharepoint.com/:u:/g/personal/radu_mantu_upb_ro/EXSrHQMCkWBEpGYseFEmnnABCA1hyb1oGWMUhnnHx8LIdQ?e=I0pxHg | OneDrive]]. | + | In this lab, we will study basic performance evaluation techniques used in machine learning, covering elementary concepts such as classification, regression, data fitting, clustering and much more. |
- | If you need to use VirtualBox, you can use this //.ovf// version to import the VM (just on OneDrive) | + | You will work in an environment that is easy to use, and provides a couple of tools like manipulating data and visualizing results. We will use a **Jupyter Notebook** hosted on **Google Colab**, which comes with a variety of useful tools already installed. |
- | [[https://ctipub-my.sharepoint.com/:u:/g/personal/cezar_craciunoiu_upb_ro/EZYR_YFyHx5GiHf5yBNuiyYB-zXhIaTNzJ8o8Ri2M8l5Mw?e=9qxrde | OneDrive]]. | + | |
- | There is also the option to download as a torrent {{:ep:labs:ep_win10_vm.7z.torrent.txt}}. | + | The exercises will be solved in Python, using popular libraries that are usually integrated in machine learning projects: |
- | DokuWiki is not configured to accept //.torrent// files so remove the //.txt// extension. | + | |
- | After that, you know what to do... | + | |
- | Alternatively, you can install the following on your own Windows machine: | + | * [[https://scikit-learn.org/stable/documentation.html|Scikit-Learn]]: fast model development, performance metrics, pipelines, dataset splitting |
- | * **[[https://go.microsoft.com/fwlink/?linkid=2120254 | ADK]]** - make sure to check //**Windows Performance Analyser**// and //**Windows Performance Recorder**//. | + | * [[https://pandas.pydata.org/pandas-docs/stable/|Pandas]]: data frames, csv parser, data analysis |
- | * **[[https://visualstudio.microsoft.com/downloads/ | Visual Studio Community Edition]]** - select //C++ development//. | + | * [[https://numpy.org/doc/|NumPy]]: scientific computation |
- | * **[[https://docs.microsoft.com/en-us/sysinternals/downloads/sysinternals-suite | Sysinternals suite]]** | + | * [[https://matplotlib.org/3.1.1/users/index.html|Matplotlib]]: data plotting |
- | </note> | + | As datasets, we will use some public corpora provided by the Kaggle community: |
- | <note important> | + | * [[https://www.kaggle.com/uciml/pima-indians-diabetes-database/data|Classification Dataset]] |
- | If Visual Studio prompts you with an "Expired" message, you will have to log in with your (university) account. | + | * [[https://www.kaggle.com/zaraavagyan/weathercsv|Regression dataset]] |
- | </note> | + | |
- | ===== Contents ===== | + | You can also check out these cheet sheets for fast reference to the most common libraries: |
- | {{page>:ep:labs:09:meta:nav&nofooter&noeditbutton}} | + | |
- | ===== Introduction ===== | + | **Cheat sheets:** |
- | The Windows operating system contains plenty of built-in tools to analyze its resource usage. The most famous one is probably the Windows Task Manager, as it highlights resource usage of individual processes and gives admins and users the option to kill any misbehaving ones. | + | * [[https://perso.limsi.fr/pointal/_media/python:cours:mementopython3-english.pdf)|python]] |
+ | * [[https://s3.amazonaws.com/assets.datacamp.com/blog_assets/Numpy_Python_Cheat_Sheet.pdf|numpy]] | ||
+ | * [[https://s3.amazonaws.com/assets.datacamp.com/blog_assets/Python_Matplotlib_Cheat_Sheet.pdf|matplotlib]] | ||
+ | * [[https://s3.amazonaws.com/assets.datacamp.com/blog_assets/Scikit_Learn_Cheat_Sheet_Python.pdf|sklearn]] | ||
+ | * [[https://github.com/pandas-dev/pandas/blob/master/doc/cheatsheet/Pandas_Cheat_Sheet.pdf|pandas]] | ||
- | The Performance Monitor and Resource Monitor are two additional tools that admins and experienced Windows users may use to analyze performance or any resources related issues on Windows machines. | + | <solution -hidden> |
- | + | [[https://colab.research.google.com/drive/1aeV9PGF_uxBA3FoKNMEzsiXMxjVSCcm4?usp=sharing|Solution]] | |
- | ==== 01. RAM Monitoring ==== | + | </solution> |
- | + | ||
- | Processes are dynamically allocating and using memory. Thus, it is possible to have memory allocation spikes. **Task Manager** shows you the amount of memory allocated to a process, by checking the **Peak Working Set** value in the Details tab. We can notice that the **Peak Working Set** can be sometimes significantly greater than the **Working Set**. | + | |
- | + | ||
- | ** What can we do if the system runs out of memory, but we do not know what causes this? ** | + | |
- | + | ||
- | Let's imagine a situation when a system encounters issues if some conditions are met, and these conditions can be reproduced. To find out the root cause of this problem, we can start the **Windows Performance Recorder** tool. We need to select the following profiles for performance recording: **Heap usage**, **Pool usage** and **VirtualAlloc usage**. Moreover, we need to make sure that the detail level is set to verbose and the logging mode selected is for memory. | + | |
- | + | ||
- | Following the steps below, we can highlight how such a situation should be treated: | + | |
- | + | ||
- | ^ Part 1: Emphasize memory usage for each process ^^ | + | |
- | | **1.** Run, in parallel with the Windows Performance Recorder application, a program that allocates memory for a while and then stops. || | + | |
- | | **2.** Save the capture and open it in Windows Performance Analyzer. || | + | |
- | | **3.** Right click on the Memory section and select the option to **Add all Memory graphs to Analysis View**. || | + | |
- | | **4.** Scroll down until you find the **Virtual Memory Snapshots** graph, which shows you the memory usage for all processes. || | + | |
- | + | ||
- | Going further, let's assume that we did not find any memory leaks for our program and the memory used by it gets successfully freed at the end. | + | |
- | + | ||
- | ** How can we determine which part of the program is responsible for generating a memory spike? ** | + | |
- | + | ||
- | A solution to this question can be achieved by using another tool from the Sysinternals suite, more precisely [[https://technet.microsoft.com/en-us/sysinternals/vmmap.aspx | VMMap]]. With this tool, we can view a process's memory allocations and usage. | + | |
- | + | ||
- | ^ Part 2: Emphasize memory spike of a process ^^ | + | |
- | | **1.** Go to Options -> Configure Symbols and make sure the paths are set correctly to point to the Microsoft Symbol Server and to the program's source files. || | + | |
- | | **2.** Click OK and navigate to File -> Select Process. || | + | |
- | | **3.** Select the **Launch and trace a new process** tab, choose the corresponding executable application and the directory where it will run. || | + | |
- | | **4.** Click OK and let the program run. || | + | |
- | | **5.** To view the latest memory allocations, you need to click on the **Heap** row in the upper-part of the VMMap tool, and hit F5 (refresh) from time to time. || | + | |
- | | **6.** Select one memory allocation from the bottom-part of the VMMap tool and press the **Heap Allocations** button, located in the bottom right corner. || | + | |
- | | **7.** Press the **Stack** button to observe where the allocation occurred. By clicking on the **Source** button, we can view the actual code for the allocation. || | + | |
- | + | ||
- | ==== 02. CPU Monitoring ==== | + | |
- | + | ||
- | Let's consider the same scenario as the one presented in the previous section. | + | |
- | + | ||
- | Monitoring the CPU usage presents similar issues to the ones encountered when monitoring the memory usage. Task Manager can help us to find out the current CPU usage for a process. | + | |
- | + | ||
- | **How can we catch an event when the CPU usage briefly spikes up and then goes back to normal?** | + | |
- | + | ||
- | Using Task Manager, we would need to continuously have someone watching what is happening, to catch the moment when the spike occurs. To overcome this limitation, we can use Windows Performance Recorder, by selecting the **CPU usage** profile. | + | |
- | + | ||
- | Following the steps below, we can highlight how such a situation should be treated: | + | |
- | + | ||
- | ^ Part 1: Emphasize the CPU usage of a program ^^ | + | |
- | | **1.** Run, in parallel with the Windows Performance Recorder application, a program that generates CPU usage for a while and then stops. || | + | |
- | | **2.** Save the capture and open it in Windows Performance Analyzer. || | + | |
- | | **3.** Right click on the Computation section and select the option to **Add all Computation graphs to Analysis View**. || | + | |
- | | **4.** The **CPU Usage** graph shows the impact of our program and helps us to determine who is generating this load. || | + | |
- | + | ||
- | <note> | + | |
- | To further debug this situation, as in the previous case of the memory, if the program that runs the problematic process was not written by us, we need to check whether it is useful or not. If the answer is no, we should definitely stop it. In case it is useful, but the program is not ours, we can try to find an update to fix the problem, or report the issue to the producer. If the program is written by us (this course - Performance Evaluation - targets such processes developed by us), then it is important to determine what causes this problem. Unfortunately, unlike in the case of monitoring the memory usage, there is no tool that can show us the entire stack, so we need to create one. | + | |
- | </note> | + | |
- | + | ||
- | ==== 03. Network Monitoring ==== | + | |
===== Tasks ===== | ===== Tasks ===== | ||
- | <note warning> | + | ==== Google Colab Notebook ==== |
- | The password for {{:ep:laboratoare:logs2.zip | log2.zip}} and {{:ep:labs:build.zip | build.zip}} is: //parola// | + | |
- | </note> | + | |
- | + | ||
- | {{namespace>:ep:labs:09:contents:tasks&nofooter&noeditbutton}} | + | |
- | + | ||
- | ==== 01. [30p] RAM Monitoring ==== | + | |
- | + | ||
- | Using Windows Performance Recorder, run a program that allocates 1MB of memory every 100 milliseconds for a while and then stops. After the program stops, save the capture, open it in Windows Performance Analyzer and analyze the Virtual Memory Snapshots graph. What conclusion can we draw by looking at the memory usage of the process that is running our program? | + | |
- | + | ||
- | Using VMMap, inspect the memory spikes generated by running the same program. | + | |
- | + | ||
- | After installing, it requires including the //vld.h// file. When writing the code, the following functions need to be overwritten: malloc, free, new, and delete. This allows each memory allocation and deallocation to be tracked. All the detected leakages (having an allocation that is not followed by a deallocation) will be saved in a log file that can be viewed after the program stops running. In the bottom part of the screenshot shown below, it can be noticed where the allocation took place and that it is not followed by a deallocation. | + | |
- | + | ||
- | <spoiler> | + | |
- | {{ :ep:laboratoare:ep5_visualstudio-vld.jpg?400 |}} | + | |
- | </spoiler> | + | |
- | + | ||
- | <hidden> | + | |
- | * It can be noticed the steep line representing the increase of our process's memory usage. This leads to the conclusion that this process is the cause of the problem since all the other ones seem stable. | + | |
- | * So far, it was identified the process that causes problems. If this process is not written by you, you can check who is launching it. If you don't need it, you can deactivate it. If you need it, check if there are any updates to fix it. If there aren't any updates to fix the problem, you can try reporting the problem to the producer and hope they will fix it. | + | |
- | </hidden> | + | |
- | + | ||
- | ==== 02. [30p] CPU Monitoring ==== | + | |
- | + | ||
- | Open EvenimenteProcMon, which has the purpose of integrating your messages with Process Monitor, so these can be viewed as the process unfolds. It is necessary to understand any code, not perfectly, but at least to get the big picture of what is going on. | + | |
- | + | ||
- | A ProcessMonitor class with 5 functions was created: | + | |
- | + | ||
- | * **OpenProcMon** opens up a handle for the Process Monitor's message interface. | + | |
- | * **CloseProcMon** closes this handle. | + | |
- | * **ProcMonLog** writes the message that is passed as a parameter to the Process Monitor interface. | + | |
- | * **MyProcMon** is the class constructor. It is called when a MyProcMon object is declared. | + | |
- | * **~ MyProcMon** is the class destructor. It is called to destroy the MyProcMon object. | + | |
- | + | ||
- | The code below highlights that it was declared globally: | + | |
- | + | ||
- | <code> | + | |
- | MyProcMon __procMon; | + | |
- | </code> | + | |
- | + | ||
- | This means that at the start of the process, before executing the main function, when the global variables are initialized, our class instance will be constructed along with the implicit handle for the Process Monitor message interface. The handle is closed when the object is destroyed, after the program's execution ends. | + | |
- | + | ||
- | Another class was declared, ProcMonLogFunc, with the purpose of highlighting when entering and leaving a function. This led to defining the following macro, which declares a ProcMonLogFunc object and passes it the name of the current function as a parameter. | + | |
- | + | ||
- | <code> | + | |
- | #define DBGTRACE_FN_ () ProcMonLogFunc __my_log __ (__ FUNCTIONW__) | + | |
- | </code> | + | |
- | + | ||
- | :!: Start Process Monitor and change the filter to **ProcessName contains EvenimenteProcMon**. Select the profiling button as shown below: | + | |
- | + | ||
- | {{ :ep:laboratoare:ep5_butonprofiling.png?400 |}} | + | |
- | + | ||
- | After running the program, the Process Monitor tool will generate a capture containing multiple details. We should notice messages such as Output: ==> Func1 and Output: <== Func1, with the associated timestamps for these events in the Time of Day column. The difference between these times indicates how long the execution took for Func1, expressed in hundreds of nanoseconds. | + | |
- | + | ||
- | As it is inefficient to calculate by hand the times for each function, we can save the output in a .csv format, by going to File -> Save and choosing the "Comma-Separated Values" option. The generated file will look like this: | + | |
- | + | ||
- | <code> | + | |
- | "4:42:07.1846936 PM","EvenimenteProcMon.exe","6352","Debug Output Profiling","","","Output: ==>main" | + | |
- | "4:42:07.1848812 PM","EvenimenteProcMon.exe","6352","Debug Output Profiling","","","Output: Acesta e logul meu 1" | + | |
- | "4:42:07.1848883 PM","EvenimenteProcMon.exe","6352","Debug Output Profiling","","","Output: ==>Func1" | + | |
- | "4:42:07.1848955 PM","EvenimenteProcMon.exe","6352","Debug Output Profiling","","","Output: <==Func1" | + | |
- | "4:42:07.1848990 PM","EvenimenteProcMon.exe","6352","Debug Output Profiling","","","Output: ==>Func2" | + | |
- | "4:42:07.1849038 PM","EvenimenteProcMon.exe","6352","Debug Output Profiling","","","Output: <==Func2" | + | |
- | "4:42:07.1849069 PM","EvenimenteProcMon.exe","6352","Debug Output Profiling","","","Output: ==>Func3" | + | |
- | "4:42:07.1849105 PM","EvenimenteProcMon.exe","6352","Debug Output Profiling","","","Output: <==Func3" | + | |
- | "4:42:07.1849148 PM","EvenimenteProcMon.exe","6352","Debug Output Profiling","","","Output: Acesta e logul meu 2" | + | |
- | "4:42:07.1849184 PM","EvenimenteProcMon.exe","6352","Debug Output Profiling","","","Output: <==main" | + | |
- | </code> | + | |
- | + | ||
- | :!: Create a simple parser in Python to find out easier the total time spent in every function. If you only want to take into account the CPU usage, you need to have logging messages before and after every I/O operation, in order to not count in their time. | + | |
- | + | ||
- | ==== 03. [40p] Network Monitoring ==== | + | |
- | + | ||
- | == Task A [20p] - Go through tutorial == | + | |
- | + | ||
- | == Task Manager == | + | |
- | The amount of network traffic generated by a process can be seen using Task Mananger. | + | |
- | + | ||
- | <spoiler> | + | |
- | {{ :ep:laboratoare:ep5_taskmanagernetworking.jpg?400 |}} | + | |
- | </spoiler> | + | |
- | + | ||
- | == Windows Performance Recorder == | + | |
- | The resources for this tutorial include Winhttp.exe, a program that downloads putty.exe. The above screenshot displays its network activity. However, if the process generating the network activity is unknown, you can use Windows Performance Recorder with the following settings. Save and open the capture to view it. The statistics offered by Windows Performance Analyzer are for the total use of the network, rather than per process statistics. | + | |
- | + | ||
- | <spoiler> | + | |
- | {{ :ep:laboratoare:ep5_wpr-cpustart.jpg?400 |}} | + | |
- | </spoiler> | + | |
- | + | ||
- | == Microsoft Network Monitoring == | + | |
- | For this reason, we are calling upon another tool developed by Microsoft. Install it, start it using "Run as administrator", and select the network interface through which the traffic is expected to pass (cable, wifi, ...). You should get a capture such as this one: | + | |
- | + | ||
- | <spoiler> | + | |
- | {{ :ep:laboratoare:ep5_netmon.jpg?400 |}} | + | |
- | </spoiler> | + | |
- | + | ||
- | == Wireshark == | + | |
- | As in the case of the CPU, inspecting the events taking place on the network involves some amount of work for the analyst. However, this being a simple case, you can just expand the view on the traffic generated by Winhttp.exe, and notice the request for //putty.exe//. If it is not clear why some requests are there or why they last so long, you can integrate the application that you wish to investigate with ProcessMonitor. This way you can insert logging elements to find out what request are made and how long they take. The part with timing the requests and traffic can be determined straight from Network Monitor by considering the times of the packets. For displaying all traffic on a http connection (it can also be https as long as you control the server, but this in not in the scope of this tutorial), you can use another tool, [[https://www.wireshark.org/download.html | Wireshark]]. Install Wireshark (**64bit!!!**) accepting the default settings. Start it and select the interface that you want to listen to. | + | |
- | + | ||
- | <spoiler> | + | |
- | {{ :ep:laboratoare:ep5_wireshark-start.jpg?400 |}} | + | |
- | </spoiler> | + | |
- | Click the //Start// button and run Winhttp.exe. After Winhttp.exe stops, click the Stop button in Wireshark. | + | For this lab, we will use Google Colab for exploring performance evaluation in machine learning. Please solve your tasks [[https://github.com/vladastefanescu/machine-learning-introduction/blob/main/Machine_Learning_Introduction.ipynb|here]] by clicking "**Open in Colaboratory**". |
- | <spoiler> | + | You can then export this python notebook as a PDF (**File -> Print**) and upload it to **Moodle**. |
- | {{ :ep:laboratoare:ep5_wireshark-captura.jpg?400 |}} | + | |
- | </spoiler> | + | |
- | This way you have obtained a traffic capture while winhttp.exe was running. Viewing the code for winhttp.exe, it can be noticed that it makes a request to www.sociouman-usamvb.ro. Use the ping command to get the IP address for this url. | + | ===== Feedback ===== |
- | <spoiler> | + | Please take a minute to fill in the **[[https://forms.gle/NpSRnoEh9NLYowFr5 | feedback form]]** for this lab. |
- | {{ :ep:laboratoare:ep5_findip.jpg?400 |}} | + | |
- | </spoiler> | + | |
- | Switching back to Wireshark, add a filter for ip.addr = 86.106.30.115 (make sure to use the IP address identified using ping command). Right click Get documents and choose Follow TCP Stream. | ||
- | <spoiler> | ||
- | {{ :ep:laboratoare:ep5_wireshark-captura2.jpg?400 |}} | ||
- | </spoiler> | ||
- | In the bottom part of the Wireshark window, at the "//Show and save data as//" option choose "Raw". Save the capture (using the "Save as" button) as "//my.pdf//". | ||
- | <spoiler> | ||
- | {{ :ep:laboratoare:ep5_wireshark-rawdata.jpg?400 |}} | ||
- | </spoiler> | ||
- | Use Notepad++ to open the my.pdf file and remove the headers as shown in the screenshot below. | ||
- | <spoiler> | ||
- | {{ :ep:laboratoare:ep5_wireshark-extractdata.jpg?400 |}} | ||
- | </spoiler> | ||
- | Save it, close Notepad++ and double-click on the newly saved file (my.pdf). | ||
- | <spoiler> | ||
- | {{ :ep:laboratoare:ep5_wireshark-extractdata-result.jpg?400 |}} | ||
- | </spoiler> | ||
- | <hidden> | ||
- | * It was possible to obtain a valid pdf file. That means that you were able to extract the conversation data from the packet exchange. This was possible due to dealing with a http communication. Otherwise, it would have been much more complicated if https was used for the communication and you would not control the server. | ||
- | </hidden> | ||
- | ==== 04. [10p] Feedback ==== | ||
- | :!: **Please take a minute to fill in the** [[https://forms.gle/KHMVUhNfCPoR71Ew7 | feedback form]] **for this lab**. | ||
- | {{ :ep:laboratoare:ep4_logo_bitd2.png?300 |}} | ||