question about out_msg.Destination in MOESI_CMP_directory response message - gem5

In MOESI_CMP_directory-L2cache.sm--the action of j_forwardGlobalRequestToLocalOwner, the "out_msg.Requestor" of the request message is filled in the machineID of that L2 cache, so it is the correct requestor.
While, to response that message in "MOESI_CMP_directory-L1cache.sm"--the action of "ee_sendDataExclusive" ,when in_msg.RequestorMachine == MachineType:L2Cache, why is the out_msg.Destination filled in "mapAddressToRange", instead of "in_msg.Requestor"? (I think it is the "out_msg.Requestor" value of the request message.)
L2 cache:
action(j_forwardGlobalRequestToLocalOwner, "j", desc="Forward external request to local owner") {
peek(requestNetwork_in, RequestMsg) {
enqueue( localRequestNetwork_out, RequestMsg, response_latency ) {
out_msg.addr := in_msg.addr;
out_msg.Type := in_msg.Type;
out_msg.Requestor := machineID;// the machineID of the request L2
out_msg.RequestorMachine := MachineType:L2Cache;
out_msg.Destination.add(getLocalOwner(cache_entry, in_msg.addr));
out_msg.Type := in_msg.Type;
out_msg.MessageSize := MessageSizeType:Forwarded_Control;
out_msg.Acks := 0 - 1;
}
}
}
L1 cacheļ¼š
action(ee_sendDataExclusive, "\e", desc="Send data from cache to requestor, don't keep a shared copy") {
peek(requestNetwork_in, RequestMsg) {
assert(is_valid(cache_entry));
if (in_msg.RequestorMachine == MachineType:L2Cache) {
enqueue(responseNetwork_out, ResponseMsg, request_latency) {
out_msg.addr := address;
out_msg.Type := CoherenceResponseType:DATA_EXCLUSIVE;
out_msg.Sender := machineID;
out_msg.SenderMachine := MachineType:L1Cache;
out_msg.Destination.add(mapAddressToRange(address, MachineType:L2Cache,
l2_select_low_bit, l2_select_num_bits, intToID(0)));//why do not filled in "in_msg.Requestor"?
out_msg.DataBlk := cache_entry.DataBlk;
out_msg.Dirty := cache_entry.Dirty;
out_msg.Acks := in_msg.Acks;
out_msg.MessageSize := MessageSizeType:Response_Data;
}
DPRINTF(RubySlicc, "Sending exclusive data to L2\n");
}
else {
enqueue(responseNetwork_out, ResponseMsg, request_latency) {
out_msg.addr := address;
out_msg.Type := CoherenceResponseType:DATA_EXCLUSIVE;
out_msg.Sender := machineID;
out_msg.SenderMachine := MachineType:L1Cache;
out_msg.Destination.add(in_msg.Requestor);
out_msg.DataBlk := cache_entry.DataBlk;
out_msg.Dirty := cache_entry.Dirty;
out_msg.Acks := in_msg.Acks;
out_msg.MessageSize := MessageSizeType:ResponseLocal_Data;
}
DPRINTF(RubySlicc, "Sending exclusive data to L1\n");
}
}
}

Related

How to connect to the CDP WebSocket server in Golang?

I want to send CDP commands to WebSocket server exposed by chrome browser.
For example, I create a new session on selenium and get WebSocket url as -> ws://172.20.10.2:4444/session/335d5805e9d98f3c37af004fa91e0f6e/se/cdp
Now I want to send CDP commands to this WebSocket connection and get results.
Sample client code :
package main
import (
"encoding/json"
"fmt"
"github.com/gorilla/websocket"
"log"
"net/url"
)
type message1 struct {
Id int `json:"id"`
Result Result `json:"result"`
}
type Result struct {
TargetInfo []TargetInfo `json:"targetInfos"`
}
type TargetInfo struct {
TargetId string `json:"targetId"`
}
type message2 struct {
Id int `json:"id"`
Method string `json:"method"`
Params Parameter `json:"params"`
}
type Parameter struct {
TargetId string `json:"targetId"`
Flatten bool `json:"flatten"`
}
type message3 struct {
Id int `json:"id"`
Method string `json:"method"`
SessionId string `json:"sessionId"`
Parameters EmulationParameter `json:"params"`
}
type EmulationParameter struct {
Width int `json:"width"`
Height int `json:"height"`
DeviceScaleFactor int `json:"deviceScaleFactor"`
Mobile bool `json:"mobile"`
}
type Response2 struct {
Method string `json:"method"`
Parameter SessionResponse `json:"params"`
}
type SessionResponse struct {
SessionId string `json:"sessionId"`
}
type message4 struct {
Id int `json:"id"`
Method string `json:"method"`
SessionId string `json:"sessionId"`
Parameters ScreenshotParameter `json:"params"`
}
type ScreenshotParameter struct {
Width int `json:"width,omitempty"`
}
func main() {
hostURL := "172.20.10.2:4444"
pathURL := "/session/dd44b246de3261c405cb9fcb017ab59a/se/cdp"
u := url.URL{Scheme: "ws", Host: hostURL, Path: pathURL}
c, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
if err != nil {
log.Fatal("dial:", err)
}
defer c.Close()
messageChannel := make(chan string)
go func() {
for {
_, message, err := c.ReadMessage()
//err = c.ReadJSON(p)
if err != nil {
log.Println("read:", err)
return
}
messageChannel <- string(message)
fmt.Println(string(message))
}
}()
message := "{\"id\":1,\"method\":\"Target.getTargets\"}"
//err = c.WriteJSON(websocket.TextMessage, []byte(message))
err = c.WriteMessage(websocket.TextMessage, []byte(message))
if err != nil {
log.Println("write:", err)
return
}
response := <-messageChannel
fmt.Println(response)
decodedResponse := message1{}
json.Unmarshal([]byte(response), &decodedResponse)
fmt.Println(decodedResponse)
fmt.Println(decodedResponse.Result.TargetInfo[0].TargetId)
temp := message2{}
temp.Id = 2
temp.Method = "Target.attachToTarget"
temp.Params.Flatten = true
temp.Params.TargetId = decodedResponse.Result.TargetInfo[0].TargetId
event2, _ := json.Marshal(temp)
err = c.WriteMessage(websocket.TextMessage, event2)
if err != nil {
log.Println("write:", err)
return
}
response2 := <-messageChannel
fmt.Println(response2)
decodedResponse2 := Response2{}
json.Unmarshal([]byte(response2), &decodedResponse2)
fmt.Println("The session Id is")
fmt.Println(decodedResponse2.Parameter.SessionId)
width := 645
height := 9651
scale := 2
temp2 := message3{}
temp2.Id = 1
temp2.SessionId = decodedResponse2.Parameter.SessionId
temp2.Method = "Emulation.setDeviceMetricsOverride"
temp2.Parameters.DeviceScaleFactor = scale
temp2.Parameters.Height = height
temp2.Parameters.Width = width
temp2.Parameters.Mobile = false
event3, _ := json.Marshal(temp2)
err = c.WriteMessage(websocket.TextMessage, event3)
if err != nil {
log.Println("write:", err)
return
}
response3 := <-messageChannel
fmt.Println(response3)
temp3 := message4{}
temp3.Id = 1
temp3.SessionId = decodedResponse2.Parameter.SessionId
temp3.Method = "Page.captureScreenshot"
event4, _ := json.Marshal(temp3)
err = c.WriteMessage(websocket.TextMessage, event4)
if err != nil {
log.Println("write:", err)
return
}
response4 := <-messageChannel
fmt.Println(response4)
}
I wasn't able to reuse this part of the code to send CDP commands to the WebSocket. I want a simple example CDP command.

how can I achieve faster mariadb inserts

I am dealing with a bit over 15 billion rows of data in various text files. I am trying to insert them into MariaDB using golang. Golang is a fast language and is often used for big data but I cannot get more than 10k-15k inserts a second, at this rate its gonna take over 15 days, I need this data imported sooner than that. I have tried various batch sizes but they all give about the same results.
function I'm using to handle file data:
func handlePath(path string) {
file, err := os.Open(path)
if err != nil {
fmt.Printf("error opening %v: %v", path, err)
return
}
defer file.Close()
scanner := bufio.NewScanner(file)
var temp_lines []string
for scanner.Scan() {
if len(temp_lines) == line_batch {
insertRows(temp_lines)
temp_lines = []string{}
}
temp_lines = append(temp_lines, scanner.Text())
}
insertRows(temp_lines)
fmt.Printf("\nFormatted %v\n", path)
if err := scanner.Err(); err != nil {
fmt.Printf("\nScanner error %v\n", err)
return
}
}
function I'm using for inserting:
func insertRows(rows []string) {
var Args []string
for _, row := range rows {
line_split := strings.Split(row, "|")
if len(line_split) != 6 {return}
database_id := line_split[0]
email := line_split[1]
password := line_split[2]
username := line_split[3]
ip := line_split[4]
phone := line_split[5]
arg := fmt.Sprintf("('%v','%v','%v','%v','%v','%v')",database_id,email,password,username,ip,phone)
Args = append(Args, arg)
}
sqlQuery := fmt.Sprintf("INSERT INTO new_table (database_id, email, password, username, ip, phone_number) VALUES %s", strings.Join(Args, ","))
_, err := db.Exec(sqlQuery)
if err != nil {
//fmt.Printf("%v\n", err)
return
}
total+=line_batch
writes++
}
Server specs:
server

Indy 10 SSL root certificate

I am trying to verify server certificate. I use Indy 10 and OpenSSL. I specify Root RootCertFile and VerifyDepth to MaxInt. OnVerifyPeer works fine - AOk is true. I wonder how to load certificates from Windows Trusted Root Certification Authorities. There is my stripped code of a client:
uses
{Delphi}
IdSSLOpenSSL
, IdHTTP
, IdHeaderList
, System.Classes
{Project}
;
type
TUnicodeHTTPPoster = class
strict private
FidHTTP: TIdHTTP;
FLastError: string;
FCertPassword: string;
procedure OnGetPassword(var Password: string);
function OnVerifySSLPeer(Certificate: TIdX509;AOk: Boolean; ADepth, AError: Integer): Boolean;
public
constructor Create(const ASSLVersion: TIdSSLVersion; const AAccept: string = 'application/xml';
const ACharSet: string = 'utf-8'; const ACertFile: string = ''; const AKeyFile: string = '';
const ACertPassword: string = ''); reintroduce;
destructor Destroy; override;
function Post(const ACustomHeaders: TIdHeaderList; const ARawBody: TStream;
const AURL: string; out AResponse: string): integer;
end;
implementation
uses
{Delphi}
System.SysUtils
, IdURI
, IdGlobal
{Project}
;
constructor TUnicodeHTTPPoster.Create(const ASSLVersion: TIdSSLVersion; const AAccept: string = 'application/xml';
const ACharSet: string = 'utf-8'; const ACertFile: string = ''; const AKeyFile: string = '';
const ACertPassword: string = '');
var
_IdSSLIOHandlerSocketOpenSSL: TIdSSLIOHandlerSocketOpenSSL;
begin
inherited Create;
FidHTTP := TIdHTTP.Create(nil);
FidHTTP.Request.Accept := 'application/xml';
if AAccept <> '' then
FidHTTP.Request.Accept := AAccept;
FidHTTP.Request.Charset := 'utf-8';
if ACharSet <> '' then
FidHTTP.Request.Charset := ACharSet;
_IdSSLIOHandlerSocketOpenSSL := TIdSSLIOHandlerSocketOpenSSL.Create(FidHTTP);
if FileExists(ACertFile) then
_IdSSLIOHandlerSocketOpenSSL.SSLOptions.CertFile := ACertFile;
if FileExists(AKeyFile) then
_IdSSLIOHandlerSocketOpenSSL.SSLOptions.KeyFile := AKeyFile;
FCertPassword := ACertPassword;
FidHTTP.Request.BasicAuthentication := False;
_IdSSLIOHandlerSocketOpenSSL.SSLOptions.Mode := sslmClient;
_IdSSLIOHandlerSocketOpenSSL.SSLOptions.Method := ASSLVersion;
_IdSSLIOHandlerSocketOpenSSL.OnGetPassword := OnGetPassword;
_IdSSLIOHandlerSocketOpenSSL.SSLOptions.VerifyMode := [sslvrfPeer];
_IdSSLIOHandlerSocketOpenSSL.SSLOptions.VerifyDepth := MaxInt;
_IdSSLIOHandlerSocketOpenSSL.OnVerifyPeer := OnVerifySSLPeer;
_IdSSLIOHandlerSocketOpenSSL.SSLOptions.RootCertFile := 'C:\Users\ekolesnikovics\Desktop\Projects\nDentity\ndentify\Build\dc_ofisas.nsoft.lt.pem';
FidHTTP.IOHandler := _IdSSLIOHandlerSocketOpenSSL;
end;
function TUnicodeHTTPPoster.OnVerifySSLPeer(Certificate: TIdX509;AOk: Boolean; ADepth, AError: Integer): Boolean;
begin
Result := AOk;
end;
procedure TUnicodeHTTPPoster.OnGetPassword(var Password: string);
begin
Password := FCertPassword;
end;
function TUnicodeHTTPPoster.Post(const ACustomHeaders: TIdHeaderList; const ARawBody: TStream;
const AURL: string; out AResponse: string): integer;
var
_URL: string;
_ResponseStream: TStringStream;
begin
Result := 500;
FLastError := '';
try
if Trim(AURL) = '' then
raise EArgumentException.Create('URL is not provided.');
_URL := TIdURI.URLEncode(AURL, IndyTextEncoding_UTF8);
_ResponseStream := TStringStream.Create('', TEncoding.UTF8);
try
if Assigned(FidHTTP.Request.CustomHeaders) then
FidHTTP.Request.CustomHeaders.Clear;
if Assigned(ACustomHeaders) then
FidHTTP.Request.CustomHeaders := ACustomHeaders;
FidHTTP.Post(_URL, ARawBody, _ResponseStream);
_ResponseStream.Position := 0;
AResponse := _ResponseStream.DataString;
finally
FreeAndNil(_ResponseStream);
end;
Result := 200;
except
on E: EIdHTTPProtocolException do
begin
Result := E.ErrorCode;
FLastError := E.ErrorMessage;
FidHTTP.Disconnect;
end;
on E: Exception do
begin
FLastError := E.Message;
FidHTTP.Disconnect;
end;
end;
end;
I ended up using free https://github.com/magicxor/WinCryptographyAPIs
procedure TUnicodeHTTPPoster.ExportWindowsCertificateStoreToFile(const ACertFile: string);
var
_hStore: HCERTSTORE;
_CertContext: PCertContext;
_pchString: Cardinal;
_szString: string;
_CertList: TStringList;
begin
_hStore := CertOpenSystemStore(0, PChar('ROOT'));
if (_hStore = nil) then
RaiseLastOSError;
_CertList := TStringList.Create;
try
_CertContext := CertEnumCertificatesInStore(_hStore, nil);
if (_CertContext = nil) then
RaiseLastOSError;
while _CertContext <> nil do
begin
_pchString := 0;
if not CryptBinaryToString(_CertContext.pbCertEncoded,
_CertContext.cbCertEncoded, CRYPT_STRING_BASE64, nil, _pchString) then
RaiseLastOSError;
SetLength(_szString, 0);
SetLength(_szString, _pchString - 1);
if not CryptBinaryToString(_CertContext.pbCertEncoded,
_CertContext.cbCertEncoded, CRYPT_STRING_BASE64, PWideChar(_szString),
_pchString) then
RaiseLastOSError;
_CertList.Add('-----BEGIN CERTIFICATE-----');
_CertList.Add(Trim(StrPas(PWideChar(_szString))));
_CertList.Add('-----END CERTIFICATE-----');
_CertContext := CertEnumCertificatesInStore(_hStore, _CertContext);
end;
_CertList.SaveToFile(ACertFile);
finally
FreeAndNil(_CertList);
CertCloseStore(_hStore, 0);
end;
end;
_RootCertFileName := TPath.Combine(ExtractFilePath(ParamStr(0)), 'windows_cert.pem');
ExportWindowsCertificateStoreToFile(_RootCertFileName);
_IdSSLIOHandlerSocketOpenSSL.SSLOptions.RootCertFile := _RootCertFileName;

Monitor spawned processes (process tree) from AHK

I've been serching (search engine, SO, AHK forums) without success how to get the PIDs of the hierarchy tree of anything ran by Run[Wait] command.
Any help/directiono will be highly appreciated.
With 20 iterations looking for a 30-child process tree my average times were:
DLL: 0.2587435
WMI: 0.1113015
Here's the bench:
pid := 7880
loops := 20
time_dll := 0
Loop, % loops
{
QPC(1)
tree_dll := processTree(pid)
time_dll += QPC()
}
time_dll /= loops
time_wmi := 0
Loop, % loops
{
QPC(1)
tree_wmi := processTree(pid, True)
time_wmi += QPC()
}
time_wmi /= loops
MsgBox,,>, % "DLL: " time_dll "`nWMI: " time_wmi
;-------------------------------------------------------------------------------
processTree(pid, wmi := False, obj := False)
{
Local
tree := []
If wmi
{
If !obj
obj := ComObjGet("winmgmts:").ExecQuery("SELECT ProcessId,ParentProcessId FROM Win32_Process")
tree := []
For result in obj
If pid = result.ParentProcessId
{
tree.Push(result.ProcessId)
subs := %A_ThisFunc%(result.ProcessId, True, obj)
For idx,val in subs
tree.Push(val)
}
}
Else
{
Static MAX_PATH := 260 << !!A_IsUnicode
Static varCapacity := MAX_PATH
VarSetCapacity(lppe, varCapacity)
NumPut(varCapacity, lppe, "UInt")
hSnapshot := DllCall("CreateToolhelp32Snapshot", "UInt",0x2, "UInt",pid, "Ptr")
DllCall("Process32First", "Ptr",hSnapshot, "Ptr",&lppe)
Loop
{
parent := NumGet(lppe, 16 + A_PtrSize * 2, "UInt")
If (parent = pid)
{
child := NumGet(lppe, 8, "UInt")
tree.Push(child)
gchilds := %A_ThisFunc%(child)
For idx,val in gchilds
tree.Push(val)
}
} Until !DllCall("Process32Next", "Ptr",hSnapshot, "Ptr",&lppe)
DllCall("CloseHandle", "Ptr",hSnapshot)
}
Return, tree
}
QPC(start := 0)
{
Static P := 0, F := 0, Q := DllCall("QueryPerformanceFrequency", "Int64P",F)
Return !DllCall("QueryPerformanceCounter", "Int64P",Q) + (start ? (P:=Q)/F : (Q-P)/F)
}

Error when add a query parameter to postgres query

When I write the code:
err := database.QueryRow("SELECT page_title,page_content,page_date FROM pages WHERE id=1").
Scan(&thisPage.Title, &thisPage.Content, &thisPage.Date)
Everything works fine. But I want it to not just get the page with id=1, but to be dynamic.
So I write:
err := database.QueryRow("SELECT page_title,page_content,page_date FROM pages WHERE id=?", pageID).
Scan(&thisPage.Title, &thisPage.Content, &thisPage.Date)
But I get an error:
GolangdProjects go run test.go
1
2017/04/13 11:29:57 Couldn't get page: 1
exit status 1
The full code:
package main
import (
"database/sql"
"fmt"
_ "github.com/lib/pq"
"github.com/gorilla/mux"
"log"
"net/http"
)
const (
DBHost = "localhost"
DBPort = ":5432"
DBUser = "nirgalon"
DBPass = ""
DBDbase = "cms"
PORT = ":8080"
)
var database *sql.DB
type Page struct {
Title string
Content string
Date string
}
func ServePage(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
pageID := vars["id"]
thisPage := Page{}
fmt.Println(pageID)
err := database.QueryRow("SELECT page_title,page_content,page_date FROM pages WHERE id=1").Scan(&thisPage.Title, &thisPage.Content, &thisPage.Date)
if err != nil {
log.Fatal("Couldn't get page: " + pageID)
log.Fatal(err.Error)
}
html := `<html><head><title>` + thisPage.Title + `</title></head><body><h1>` + thisPage.Title + `</h1><div>` + thisPage.Content + `</div></body></html>`
fmt.Fprintln(w, html)
}
func main() {
db, err := sql.Open("postgres", "user=nirgalon dbname=cms sslmode=disable")
if err != nil {
log.Println("Couldn't connnect to db")
log.Fatal(err)
}
database = db
routes := mux.NewRouter()
routes.HandleFunc("/page/{id:[0-9]+}", ServePage)
http.Handle("/", routes)
http.ListenAndServe(PORT, nil)
}
psql driver uses $1, $2, etc for params:
database.QueryRow("SELECT page_title,page_content,page_date FROM pages WHERE id = $1", pageID)
QueryRow takes var args of type interface{}. It could be that pageID is being interpolated as a string (as returned by mux.Vars), resulting in a query that's incorrectly formatted as:
"SELECT page_title,page_content,page_date FROM pages WHERE id='1'"
Try converting pageID to int:
id := vars["id"]
pageID := strconv.Atoi(id)