streamlet-examples/pid.cpp
branchv_0
changeset 64 7ba9d703fadb
equal deleted inserted replaced
63:8c6885543e2c 64:7ba9d703fadb
       
     1 /**
       
     2  * Relational pipes
       
     3  * Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
       
     4  *
       
     5  * This program is free software: you can redistribute it and/or modify
       
     6  * it under the terms of the GNU General Public License as published by
       
     7  * the Free Software Foundation, version 3 of the License.
       
     8  *
       
     9  * This program is distributed in the hope that it will be useful,
       
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
       
    12  * GNU General Public License for more details.
       
    13  *
       
    14  * You should have received a copy of the GNU General Public License
       
    15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
       
    16  */
       
    17 
       
    18 #include "streamlet-common.h"
       
    19 
       
    20 #include <unistd.h>
       
    21 
       
    22 /**
       
    23  * This streamlet is useful only for debugging or study purposes.
       
    24  * 
       
    25  * It provides two attributes:
       
    26  *  - pid_streamlet: getpid()
       
    27  *  - pid_worker: getppid()
       
    28  *	
       
    29  * Can be used for studying how the parallelism works:
       
    30  *  - Each instance of streamlet (each --streamlet pid) will have different pid_streamlet.
       
    31  *  - Each worker (their number = X in --parallelism X) will have different pid_worker.
       
    32  * 
       
    33  * So we can check how evenly the work has been distributed across processes (e.g. do some GROUP BY in SQL).
       
    34  */
       
    35 class PidStreamlet : public Streamlet {
       
    36 
       
    37 	std::vector<AttributeMetadata> getOutputAttributesMetadata() override {
       
    38 		std::vector<AttributeMetadata> oam;
       
    39 		int i = 0;
       
    40 		oam.push_back({getAlias(i++, L"pid_streamlet"), INTEGER});
       
    41 		oam.push_back({getAlias(i++, L"pid_worker"), INTEGER});
       
    42 		return oam;
       
    43 	}
       
    44 
       
    45 	std::vector<OutputAttribute> getOutputAttributes() override {
       
    46 		std::vector<OutputAttribute> oa;
       
    47 		oa.push_back({std::to_wstring(getpid()), false});
       
    48 		oa.push_back({std::to_wstring(getppid()), false});
       
    49 		return oa;
       
    50 	}
       
    51 };
       
    52 
       
    53 STREAMLET_RUN(PidStreamlet)